Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic Random Function in Swift

I have researched and looked into many different random function, but the downside to them is that they only work for one data type. I want a way to have one function work for a Double, Floats, Int, CGFloat, etc. I know I could just convert it to different data types, but I would like to know if there is a way to have it done using generics. Does someone know a way to make a generic random function using swift. Thanks.

like image 989
grahamcracker1234 Avatar asked Mar 13 '23 20:03

grahamcracker1234


1 Answers

In theory you could port code such as this answer (which is not necessarily a great solution, depending on your needs, since UInt32.max is relatively small)

extension FloatingPointType {
    static func rand() -> Self {
        let num = Self(arc4random())
        let denom = Self(UInt32.max)
        // this line won’t compile:
        return num / denom
    }
}

// then you could do
let d = Double.rand()
let f = Float.rand()
let c = CGFloat.rand()

Except… FloatingPointType doesn’t conform to a protocol that guarantees operators like /, + etc (unlike IntegerType which conforms to _IntegerArithmeticType).

You could do this manually though:

protocol FloatingPointArithmeticType: FloatingPointType {
    func /(lhs: Self, rhs: Self) -> Self
    // etc
}

extension Double: FloatingPointArithmeticType { }
extension Float: FloatingPointArithmeticType { }
extension CGFloat: FloatingPointArithmeticType { }

extension FloatingPointArithmeticType {
    static func rand() -> Self {
        // same as before
    }
}

// so these now work
let d = Double.rand()  // etc

but this probably isn’t quite as out-of-the-box as you were hoping (and no doubt contains some subtle invalid assumption about floating-point numbers on my part).

As for something that works across both integers and floats, the Swift team have mentioned on their mail groups before that the lack of protocols spanning both types is a conscious decision since it’s rarely correct to write the same code for both, and that’s certainly the case for generating random numbers, which need two very different approaches.

like image 140
Airspeed Velocity Avatar answered Mar 20 '23 02:03

Airspeed Velocity