Seeded random numbers with upper bound in Swift 3?

394 views Asked by At

Is there any way to seed the random number generator and also specify an upper bound in Swift 3?

1

There are 1 answers

0
Code Different On

The most convenient random number generators in Swift reside in GameKit. This generates random integers between -10 and 10 (inclusive):

import GameKit
let source = GKARC4RandomSource(seed: Data(bytes: [42]))
let random = GKRandomDistribution(randomSource: source, lowestValue: -10, highestValue: 10)

for _ in 0..<10 {
    print(random.nextInt())
}