Skip to content

Instantly share code, notes, and snippets.

@skreutzberger
Created August 12, 2015 15:16
Show Gist options
  • Save skreutzberger/1563f967f7a6afcc7e04 to your computer and use it in GitHub Desktop.
Save skreutzberger/1563f967f7a6afcc7e04 to your computer and use it in GitHub Desktop.
extension to generate random Int in Swift
extension Int {
static func random(max: Int) -> Int {
let rnd = Int(arc4random_uniform(UInt32(max) + 1))
return rnd
}
}
@SashaTsebrii
Copy link

extension Int {
    static func random(min: Int, max: Int) -> Int {
        precondition(min <= max)
        let randomizer = GKRandomSource.sharedRandom()
        return min + randomizer.nextInt(upperBound: max - min + 1)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment