Skip to content

Instantly share code, notes, and snippets.

@tiktuk
tiktuk / Random.swift
Last active Oct 8, 2015 — forked from JadenGeller/Random.swift
Random Numbers in Swift
View Random.swift
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
}
You can’t perform that action at this time.