gpt4 book ai didi

go - 为什么每次运行程序时 math/rand 包中的 rand.Intn() 函数都会生成相同的随机数序列?

转载 作者:数据小太阳 更新时间:2023-10-29 03:34:46 27 4
gpt4 key购买 nike

我使用 rand.Intn(n int) 函数交换 slice 内的元素,但每次运行该程序时,输出都是 slice 内相同的随机元素序列。

我在这里缺少什么明显的东西?

最佳答案

来自 documentation

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com