有没有办法播种随机数生成器并在Swift 3中指定上限?
答案 0 :(得分:0)
Swift中最方便的随机数生成器驻留在GameKit中。这会生成-10到10(含)之间的随机整数:
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())
}