Skip to content

Instantly share code, notes, and snippets.

@natecook1000
Created October 9, 2014 16:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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