Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Random integers without shuttling between UInt32 and Int
func arc4random_uniform<T: SignedIntegerType>(max: T) -> T {
let max32: Int32 = numericCast(max)
return T(Int64(arc4random_uniform(UInt32(max32))))
}
func arc4random_uniform<T: UnsignedIntegerType>(max: T) -> T {
let max32: UInt32 = numericCast(max)
return T(UInt64(arc4random_uniform(max32)))
}
let max = 20
for _ in 1...5 {
// look, a random Int:
let randomInt: Int = arc4random_uniform(max)
println(randomInt)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.