Created
August 15, 2017 08:09
-
-
Save michaelevensen/6016ff2300a4b88bbf655726268d2db1 to your computer and use it in GitHub Desktop.
Random number between range.
This file contains hidden or 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
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