Skip to content

Instantly share code, notes, and snippets.

@zmeyc
Forked from jstn/RandomNumbers.swift
Last active December 7, 2015 12:15
Show Gist options
  • Save zmeyc/762b33b8428e8a0fface to your computer and use it in GitHub Desktop.
Save zmeyc/762b33b8428e8a0fface to your computer and use it in GitHub Desktop.
// Merged other forks and updated to Swift 2.0.
// Original code:
// https://gist.github.com/haipham/d169cdea2f28222db7f3
// http://stackoverflow.com/questions/24007129/how-does-one-generate-a-random-number-in-apples-swift-language
// https://gist.github.com/pontusarmini/169bc5b3c3fc552a4c3d
import Foundation
import CoreGraphics
// Suppress a warning
let dynamicWordSize = __WORDSIZE
public func arc4random<T: IntegerLiteralConvertible>(type: T.Type) -> T {
var r: T = 0
arc4random_buf(&r, Int(sizeof(T)))
return r
}
public extension UInt64 {
public static func random(lower 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 % u) + lower
}
}
public extension Int64 {
public static func random(lower 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))
} else {
return Int64(r) + lower
}
}
}
public extension UInt32 {
static func random(lower lower: UInt32 = min, upper: UInt32 = max) -> UInt32 {
return arc4random_uniform(upper - lower) + lower
}
}
public extension Int32 {
public static func random(lower lower: Int32 = min, upper: Int32 = max) -> Int32 {
let r = arc4random_uniform(UInt32(Int64(upper) - Int64(lower)))
return Int32(Int64(r) + Int64(lower))
}
}
public extension UInt {
public static func random(lower lower: UInt = min, upper: UInt = max) -> UInt {
switch dynamicWordSize {
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
}
}
}
public extension Int {
public static func random(lower lower: Int = min, upper: Int = max) -> Int {
switch dynamicWordSize {
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
}
}
}
public extension Float {
public static func random(lower 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 lower: Double = 0.0, upper: Double = 1.0) -> Double {
let r = Double(arc4random(UInt64)) / Double(UInt64.max)
return (r * (upper - lower)) + lower
}
}
public extension CGFloat {
public static func random(lower lower: CGFloat = 0.0, upper: CGFloat = 1.0) -> CGFloat
{
switch dynamicWordSize {
case 32:
let r = CGFloat(arc4random(UInt32)) / CGFloat(UInt32.max)
return (r * (upper - lower)) + lower
case 64:
let r = CGFloat(arc4random(UInt64)) / CGFloat(UInt64.max)
return (r * (upper - lower)) + lower
default: return lower
}
}
}
public extension CGPoint {
public static func randomWithinCGRect(rectangle: CGRect) -> CGPoint
{
let x = CGFloat.random(lower: rectangle.minX, upper: rectangle.maxX)
let y = CGFloat.random(lower: rectangle.minY, upper: rectangle.maxY)
return CGPointMake(x,y)
}
public static func randomWithinCGSize(size: CGSize) -> CGPoint
{
let x = CGFloat.random(lower: 0.0, upper: size.width)
let y = CGFloat.random(lower: 0.0, upper: size.height)
return CGPointMake(x, y)
}
public static func random(minX minX: CGFloat, maxX: CGFloat, minY: CGFloat, maxY: CGFloat) -> CGPoint
{
return CGPoint.randomWithinCGRect(CGRectMake(minX, minY, maxX - minX, maxY - minY))
}
public static func random(withinRadius radius: CGFloat, ofPoint midPoint:CGPoint) -> CGPoint
{
let r = CGFloat.random(lower: 0.0, upper: radius)
let a = CGFloat.random(lower: 0.0, upper: CGFloat(M_PI * 2))
let x = midPoint.x + r * cos(a)
let y = midPoint.y + r * sin(a)
return CGPointMake(x, y)
}
}
public extension Bool {
public static func random() -> Bool
{
return Double.random() <= 0.5
}
public static func randomWithLikeliness(likeliness: Double) -> Bool
{
assert(likeliness > 0.0 && likeliness <= 1.0, "Likeliness percentage must be expressed with a value between 0.0 - 1.0")
return Double.random() <= likeliness
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment