Is there're a way to seed Swift 4.2 random number generator

北慕城南 提交于 2019-12-05 13:21:45

The whole idea of the new architecture is that any generator can be substituted just by adopting the RandomNumberGenerator protocol. So if you need a repeatable seed, use your own random generator algorithm.

If you want reproducibility via explicit seeding you can use the GameplayKit implementation of Mersenne Twister:

import Cocoa
import GameplayKit

let mt = GKMersenneTwisterRandomSource.init(seed: 12345)

for _ in (1...5) {
  print(mt.nextUniform())
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!