Created
October 9, 2014 16:43
-
-
Save natecook1000/6b49a7b50a5184a173c7 to your computer and use it in GitHub Desktop.
Random integers without shuttling between UInt32 and Int
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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