Skip to content

Instantly share code, notes, and snippets.

@bananafish911
Created October 15, 2016 09:26
Show Gist options
  • Save bananafish911/05abb10d079377c65cbf64037b2a574a to your computer and use it in GitHub Desktop.
Save bananafish911/05abb10d079377c65cbf64037b2a574a to your computer and use it in GitHub Desktop.
extension Int {
/// SwiftRandom extension
static func random(lower: Int = 0, _ upper: Int = 100) -> Int {
return lower + Int(arc4random_uniform(UInt32(upper - lower + 1)))
}
var array: [Int] {
return description.characters.map{Int(String($0)) ?? 0}
}
}
extension Double {
/// SwiftRandom extension
static func random(lower: Double = 0, _ upper: Double = 100) -> Double {
return (Double(arc4random()) / 0xFFFFFFFF) * (upper - lower) + lower
}
}
extension Float {
/// SwiftRandom extension
static func random(lower: Float = 0, _ upper: Float = 100) -> Float {
return (Float(arc4random()) / 0xFFFFFFFF) * (upper - lower) + lower
}
}
extension CGFloat {
/// SwiftRandom extension
static func random(lower: CGFloat = 0, _ upper: CGFloat = 1) -> CGFloat {
return CGFloat(Float(arc4random()) / Float(UINT32_MAX)) * (upper - lower) + lower
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment