Skip to content

Instantly share code, notes, and snippets.

@neilpa
Last active August 29, 2015 14:19
Show Gist options
  • Save neilpa/a87ceefccf19f1127781 to your computer and use it in GitHub Desktop.
Save neilpa/a87ceefccf19f1127781 to your computer and use it in GitHub Desktop.
Randomness
protocol Randomizable {
static func random() -> Self
}
protocol IntervalRandomizable: Randomizable, Comparable {
static func random(interval: HalfOpenInterval<Self>) -> Self
static func random(interval: ClosedInterval<Self>) -> Self
}
extension CGFloat: Randomizable {
static func random() -> CGFloat {
return CGFloat(arc4random() % 100) / 100
}
}
extension UIColor: Randomizable {
static func random() -> Self {
return self(red: CGFloat.random(), green: CGFloat.random(), blue: CGFloat.random(), alpha: 1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment