Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I seed a random number in Swift 3 [duplicate]

I need to start the same random number list over every execution of my app. srand/rand do not exist anymore. What should I do then?

private extension Array {
    private func randomValues(_ seed: UInt32, num: Int) -> [Element] {
        srand (seed)

        var indices = [Int]()
        indices.reserveCapacity(num)
        let range = 0..<self.count
        for _ in 0..<num {
            var random = 0
            repeat {
                random = randomNumberInRange(range)
            } while indices.contains(random)
            indices.append(random)
        }

        return indices.map { self[$0] }
    }
like image 899
Stéphane de Luca Avatar asked Nov 19 '22 18:11

Stéphane de Luca


1 Answers

You can use srand48(seed) and drand48() in Swift3.

like image 150
Lycoris Avatar answered Dec 12 '22 12:12

Lycoris