Skip to content

Instantly share code, notes, and snippets.

@michaeldorner
Forked from jstn/RandomNumbers.swift
Created December 21, 2015 18:16
Show Gist options
  • Select an option

  • Save michaeldorner/267fb29478dcdd638cde to your computer and use it in GitHub Desktop.

Select an option

Save michaeldorner/267fb29478dcdd638cde to your computer and use it in GitHub Desktop.

Revisions

  1. @jstn jstn revised this gist Jul 28, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions RandomNumbers.swift
    Original 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)
    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 arc4random(self)
    default: return lower
    }
    }
    }
  2. @jstn jstn revised this gist Jul 28, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions RandomNumbers.swift
    Original 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 0
    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 0
    default: return arc4random(self)
    }
    }
    }
  3. @jstn jstn revised this gist Jul 28, 2014. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions RandomNumbers.swift
    Original file line number Diff line number Diff line change
    @@ -55,14 +55,15 @@ public extension UInt64 {
    r = arc4random(UInt64)
    }

    return (r % upper) + lower
    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 r = UInt64.random(upper: overflow ? (UInt64.max - UInt64(~s)) : UInt64(s))
    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))
  4. @jstn jstn revised this gist Jul 28, 2014. 1 changed file with 9 additions and 14 deletions.
    23 changes: 9 additions & 14 deletions RandomNumbers.swift
    Original 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 = UInt32.random(upper: UInt32(Int64(upper) - Int64(lower)))
    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 {
    return random_uniform(upper - lower) + lower
    }

    private static func random_uniform(upper: UInt64) -> UInt64 {
    var m: UInt64
    let u = upper - lower
    var r = arc4random(UInt64)

    if upper > UInt64(Int64.max) {
    m = 1 + ~upper
    if u > UInt64(Int64.max) {
    m = 1 + ~u
    } else {
    m = ((max - (upper * 2)) + 1) % upper
    m = ((max - (u * 2)) + 1) % u
    }

    var r = arc4random(UInt64)

    while r < m {
    r = arc4random(UInt64)
    }

    return r % upper
    return (r % upper) + lower
    }
    }

    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)
    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))
  5. @jstn jstn revised this gist Jul 28, 2014. 1 changed file with 85 additions and 7 deletions.
    92 changes: 85 additions & 7 deletions RandomNumbers.swift
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,92 @@
    import Foundation
    import Darwin

    extension UInt32 {
    static func random(lower: UInt32 = 0, upper: UInt32 = UInt32.max) -> UInt32 {
    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
    }
    }

    extension Float {
    static func random(lower: Float = 0.0, upper: Float = Float(UInt32.max)) -> Float {
    let r = Float(Double(arc4random()) / Double(UInt32.max))
    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
    }
    }
  6. @jstn jstn created this gist Jun 11, 2014.
    14 changes: 14 additions & 0 deletions RandomNumbers.swift
    Original 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
    }
    }