-
-
Save michaeldorner/267fb29478dcdd638cde to your computer and use it in GitHub Desktop.
Revisions
-
jstn revised this gist
Jul 28, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -11,7 +11,7 @@ public extension UInt { switch (__WORDSIZE) { case 32: return UInt(UInt32.random(lower: UInt32(lower), upper: UInt32(upper))) case 64: return UInt(UInt64.random(lower: UInt64(lower), upper: UInt64(upper))) default: return lower } } } @@ -21,7 +21,7 @@ public extension Int { switch (__WORDSIZE) { case 32: return Int(Int32.random(lower: Int32(lower), upper: Int32(upper))) case 64: return Int(Int64.random(lower: Int64(lower), upper: Int64(upper))) default: return lower } } } -
jstn revised this gist
Jul 28, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -11,7 +11,7 @@ public extension UInt { switch (__WORDSIZE) { case 32: return UInt(UInt32.random(lower: UInt32(lower), upper: UInt32(upper))) case 64: return UInt(UInt64.random(lower: UInt64(lower), upper: UInt64(upper))) default: return arc4random(self) } } } @@ -21,7 +21,7 @@ public extension Int { switch (__WORDSIZE) { case 32: return Int(Int32.random(lower: Int32(lower), upper: Int32(upper))) case 64: return Int(Int64.random(lower: Int64(lower), upper: Int64(upper))) default: return arc4random(self) } } } -
jstn revised this gist
Jul 28, 2014 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -55,14 +55,15 @@ public extension UInt64 { r = arc4random(UInt64) } return (r % u) + lower } } public extension Int64 { public static func random(lower: Int64 = min, upper: Int64 = max) -> Int64 { let (s, overflow) = Int64.subtractWithOverflow(upper, lower) let u = overflow ? UInt64.max - UInt64(~s) : UInt64(s) let r = UInt64.random(upper: u) if r > UInt64(Int64.max) { return Int64(r - (UInt64(~lower) + 1)) -
jstn revised this gist
Jul 28, 2014 . 1 changed file with 9 additions and 14 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -34,40 +34,35 @@ public extension UInt32 { public extension Int32 { public static func random(lower: Int32 = min, upper: Int32 = max) -> Int32 { let r = arc4random_uniform(UInt32(Int64(upper) - Int64(lower))) return Int32(Int64(r) + Int64(lower)) } } public extension UInt64 { public static func random(lower: UInt64 = min, upper: UInt64 = max) -> UInt64 { var m: UInt64 let u = upper - lower var r = arc4random(UInt64) if u > UInt64(Int64.max) { m = 1 + ~u } else { m = ((max - (u * 2)) + 1) % u } while r < m { r = arc4random(UInt64) } return (r % upper) + lower } } public extension Int64 { public static func random(lower: Int64 = min, upper: Int64 = max) -> Int64 { let (s, overflow) = Int64.subtractWithOverflow(upper, lower) let r = UInt64.random(upper: overflow ? (UInt64.max - UInt64(~s)) : UInt64(s)) if r > UInt64(Int64.max) { return Int64(r - (UInt64(~lower) + 1)) -
jstn revised this gist
Jul 28, 2014 . 1 changed file with 85 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,14 +1,92 @@ import Darwin public func arc4random <T: IntegerLiteralConvertible> (type: T.Type) -> T { var r: T = 0 arc4random_buf(&r, UInt(sizeof(T))) return r } public extension UInt { public static func random(lower: UInt = min, upper: UInt = max) -> UInt { switch (__WORDSIZE) { case 32: return UInt(UInt32.random(lower: UInt32(lower), upper: UInt32(upper))) case 64: return UInt(UInt64.random(lower: UInt64(lower), upper: UInt64(upper))) default: return 0 } } } public extension Int { public static func random(lower: Int = min, upper: Int = max) -> Int { switch (__WORDSIZE) { case 32: return Int(Int32.random(lower: Int32(lower), upper: Int32(upper))) case 64: return Int(Int64.random(lower: Int64(lower), upper: Int64(upper))) default: return 0 } } } public extension UInt32 { public static func random(lower: UInt32 = min, upper: UInt32 = max) -> UInt32 { return arc4random_uniform(upper - lower) + lower } } public extension Int32 { public static func random(lower: Int32 = min, upper: Int32 = max) -> Int32 { let r = UInt32.random(upper: UInt32(Int64(upper) - Int64(lower))) return Int32(Int64(r) + Int64(lower)) } } public extension UInt64 { public static func random(lower: UInt64 = min, upper: UInt64 = max) -> UInt64 { return random_uniform(upper - lower) + lower } private static func random_uniform(upper: UInt64) -> UInt64 { var m: UInt64 if upper > UInt64(Int64.max) { m = 1 + ~upper } else { m = ((max - (upper * 2)) + 1) % upper } var r = arc4random(UInt64) while r < m { r = arc4random(UInt64) } return r % upper } } public extension Int64 { public static func random(lower: Int64 = min, upper: Int64 = max) -> Int64 { let (v,overflow) = Int64.subtractWithOverflow(upper, lower) let u = overflow ? UInt64.max - UInt64(~v) : UInt64(v) let r = UInt64.random(upper: u) if r > UInt64(Int64.max) { return Int64(r - (UInt64(~lower) + 1)) } else { return Int64(r) + lower } } } public extension Float { public static func random(lower: Float = 0.0, upper: Float = 1.0) -> Float { let r = Float(arc4random(UInt32)) / Float(UInt32.max) return (r * (upper - lower)) + lower } } public extension Double { public static func random(lower: Double = 0.0, upper: Double = 1.0) -> Double { let r = Double(arc4random(UInt64)) / Double(UInt64.max) return (r * (upper - lower)) + lower } } -
jstn created this gist
Jun 11, 2014 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ import Foundation extension UInt32 { static func random(lower: UInt32 = 0, upper: UInt32 = UInt32.max) -> UInt32 { return arc4random_uniform(upper - lower) + lower } } extension Float { static func random(lower: Float = 0.0, upper: Float = Float(UInt32.max)) -> Float { let r = Float(Double(arc4random()) / Double(UInt32.max)) return (r * (upper - lower)) + lower } }