Swift 3中带有上限的种子随机数?

时间:2017-01-01 22:09:29

标签: ios swift3 srand random-seed

有没有办法播种随机数生成器并在Swift 3中指定上限?

1 个答案:

答案 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())
}