Skip to content

Instantly share code, notes, and snippets.

@pontusarmini
Forked from jstn/RandomNumbers.swift
Last active November 10, 2015 04:39
Show Gist options
  • Save pontusarmini/169bc5b3c3fc552a4c3d to your computer and use it in GitHub Desktop.
Save pontusarmini/169bc5b3c3fc552a4c3d to your computer and use it in GitHub Desktop.
Forked from user jstn. I have added extensions to CGFloat, CGPoint and Bool.
import Darwin
public func arc4random <T: IntegerLiteralConvertible> (type: T.Type) -> T {
var r: T = 0
arc4random_buf(&r, Int(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 lower
}
}
}
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 lower
}
}
public static func random(range: Range<Int>) -> Int {
switch (__WORDSIZE) {
case 32: return Int(Int32.random(lower: Int32(range.startIndex), upper: Int32(range.endIndex)))
case 64: return Int(Int64.random(lower: Int64(range.startIndex), upper: Int64(range.endIndex)))
default: return range.startIndex
}
}
}
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 = 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 % 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))
} 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
}
}
public extension CGFloat {
public static func random(lower: CGFloat = 0.0, upper: CGFloat = 1.0) -> CGFloat
{
switch (__WORDSIZE) {
case 32:
let r = CGFloat(arc4random(UInt32)) / CGFloat(UInt32.max)
return (r * (max - min)) + min
case 64:
let r = CGFloat(arc4random(UInt64)) / CGFloat(UInt64.max)
return (r * (max - min)) + min
default: return min
}
}
}
public extension CGPoint {
public static func randomWithinCGRect(rectangle:CGRect) -> CGPoint
{
let x = CGFloat.random(min: rectangle.minX, max: rectangle.maxX)
let y = CGFloat.random(min: rectangle.minY, max: rectangle.maxY)
return CGPointMake(x,y)
}
public static func randomWithinCGSize(size:CGSize) -> CGPoint
{
let x = CGFloat.random(min: 0.0, max: size.width)
let y = CGFloat.random(min: 0.0, max: size.height)
return CGPointMake(x, y)
}
public static func random(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(min: 0.0, max: radius)
let a = CGFloat.random(min: 0.0, max: 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 {
/**
Random bool.
:returns: A random Bool.
*/
public static func random() -> Bool
{
return Double.random() <= 0.5
}
/**
Random boolean with likeliness percentage parameter. An argument of 0.3 will return true ≈30% of the times the function is called.
:param: likeliness Likeliness percentage expressed as a decimal between 0.0-1.0. 0.1 == 10% etc.
:returns: A random Bool.
*/
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
}
}
//Int
let randomInt = Int.random(1...10)
let randomInt = Int.random(lower:1, upper:10)
//Double, Float, CGFloat
let randomDouble = Double.random() // Random between 0.0-1.0
let randomDouble = Double.random(lower:2.0, upper:5.0)
//CGPoint
let randomPoint = CGPoint.random(minX:5.0, maxX:10.0, minY:20.0, maxY:30.0)
let randomPoint = CGPoint.randomWithinCGRect(rectangle)
let randomPoint = CGPoint.randomWithinCGSize(size)
let randomPoint = CGPoint.random(WithinRadius:40.0, OfPoint:point) //Random point within circle
//Bool
let randomBool = Bool.random() //Random true or false
let randomBool = Bool.randomWithLikeliness(0.3) // True in approx 30% of the cases
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment