
php小编西瓜在这里给大家分享一种解决方案,当我们使用rand.Seed(SEED)被弃用时,我们可以使用NewRand(NewSeed())来代替。NewSeed()函数可以生成一个新的种子,而NewRand()函数则可以使用这个新种子来生成随机数。这种方法可以帮助我们继续使用随机数生成功能,而不受到rand.Seed(SEED)被弃用的限制。通过这种方式,我们可以轻松地解决这个问题,并继续使用随机数生成功能。
我现在正在学习go。
我是一个例子,我有这行
rand.seed(seed)
但是关于 go 的 vscode 扩展告诉了我
rand.Seed has been deprecated since Go 1.20 and an alternative has been available since Go 1.0: Programs that call Seed and then expect a specific sequence of results from the global random source (using functions such as Int) can be broken when a dependency changes how much it consumes from the global random source. To avoid such breakages, programs that need a specific result sequence should use NewRand(NewSource(seed)) to obtain a random generator that other packages cannot access. (SA1019)
我无法理解如何按照建议使用 newrand(newsource(seed)) 。
我找到了有关 newsource 的文档 https://pkg.go.dev/math/rand#newsource
但是没有关于 newrand 函数的文档
新推荐的 rand.seed(seed) 等效项是什么?
go 1.20 种子文档有一个拼写错误。按照最新文档中的说明使用 rand.new(rand.newsource(seed)) a> 和 go 1.20 发行说明。
创建随机源并使用源上的方法而不是调用包函数:
r := rand.New(rand.NewSource(seed)) fmt.Println(r.Uint64()) fmt.Println(r.Uint64())
以上就是rand.Seed(SEED) 已弃用,如何使用 NewRand(NewSeed( ) )?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号