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
struct Random { | |
static func within<B: protocol<Comparable, ForwardIndexType>>(range: ClosedInterval<B>) -> B { | |
let inclusiveDistance = range.start.distanceTo(range.end).successor() | |
let randomAdvance = B.Distance(arc4random_uniform(UInt32(inclusiveDistance.toIntMax())).toIntMax()) | |
return range.start.advancedBy(randomAdvance) | |
} | |
static func within(range: ClosedInterval<Float>) -> Float { | |
return (range.end - range.start) * Float(Float(arc4random()) / Float(UInt32.max)) + range.start | |
} |