Skip to content

Instantly share code, notes, and snippets.

@michaelevensen
Created August 15, 2017 08:09
Show Gist options
  • Save michaelevensen/6016ff2300a4b88bbf655726268d2db1 to your computer and use it in GitHub Desktop.
Save michaelevensen/6016ff2300a4b88bbf655726268d2db1 to your computer and use it in GitHub Desktop.
Random number between range.
extension Int {
static func random(range: Range<Int>) -> Int {
var offset = 0
if range.lowerBound < 0 {
offset = abs(range.lowerBound)
}
let mini = UInt32(range.lowerBound + offset)
let maxi = UInt32(range.upperBound + offset)
return Int(mini + arc4random_uniform(maxi - mini)) - offset
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment