Skip to content

Instantly share code, notes, and snippets.

@phrz
Last active November 22, 2016 17:32
Show Gist options
  • Save phrz/823afe778556113cbe79c6e8c87ce554 to your computer and use it in GitHub Desktop.
Save phrz/823afe778556113cbe79c6e8c87ce554 to your computer and use it in GitHub Desktop.
easy random number function given a Range in Swift 3
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