我使用 rand.Intn(n int) 函数交换 slice 内的元素,但每次运行该程序时,输出都是 slice 内相同的随机元素序列。
我在这里缺少什么明显的东西?
最佳答案
Use the Seed function to initialize the default Source if different behavior is required for each run.
If Seed is not called, the generator behaves as if seeded by Seed(1).
默认情况下,每次运行都会获得相同的种子。
您可以使用当前时间作为种子来获得不确定性较低的序列。
rand.Seed(time.Now().UTC().UnixNano())
关于go - 为什么每次运行程序时 math/rand 包中的 rand.Intn() 函数都会生成相同的随机数序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47113588/