I am trying to figure out how to make the below code compile on linux. The solution for Darwin self.init(arc4random_uniform(UInt32(upperLimit))) works well, but self.init(random() % upperLimit) generates the error below.
extension Int {
/// Initializes a new `Int ` instance with a random value below a given `Int`.
///
/// - Parameters:
/// - randomBelow: The upper bound value to create a random value with.
public init?(randomBelow upperLimit: Int) {
guard upperLimit > 0 else { return nil }
#if os(Linux)
self.init(random() % upperLimit)
#else
self.init(arc4random_uniform(UInt32(upperLimit)))
#endif
}
}
Error:
error: repl.swift:11:27: error: static member 'random' cannot be used on instance of type 'Int'
self.init(random() % upperLimit)
^~~~~~
Self.
- I have a hunch this can be fixed with getters, but I wasn't able to find the solution. Any help is appreciated.