Last active
November 22, 2016 17:32
-
-
Save phrz/823afe778556113cbe79c6e8c87ce554 to your computer and use it in GitHub Desktop.
easy random number function given a Range in Swift 3
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
import Foundation | |
func random(_ r: ClosedRange<Int>) -> Int { | |
let span = abs(r.upperBound-r.lowerBound) | |
return Int(arc4random_uniform(UInt32(span)))+r.lowerBound | |
} | |
print(random(0...3)) // prints either 0, 1, 2, or 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment