Skip to content

Instantly share code, notes, and snippets.

@natecook1000
Created October 9, 2014 16:43
Show Gist options
  • Save natecook1000/6b49a7b50a5184a173c7 to your computer and use it in GitHub Desktop.
Save natecook1000/6b49a7b50a5184a173c7 to your computer and use it in GitHub Desktop.
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