gpt4 book ai didi

c# - C#随机数生成器保证稳定吗?

转载 作者:行者123 更新时间:2023-12-03 22:56:49 25 4
gpt4 key购买 nike

我正在编写一个程序,它使用大量随机数来生成内容。但我想确保当以相同的种子开始时,生成的内容是相同的。因此,当我像这样创建随机数时:

Random r = new Random(seed);
int num = r.Next();

如何保证 num 随着时间的推移始终相同(意味着:.NET 的多个版本)?后台有标准化的 list 吗?或者我应该使用我自己的“随机列表”来确保这永远不会改变?

最佳答案

不,相同种子的结果可能因框架版本而异,它是 in the documentation这里:

The implementation of the random number generator in the Random class isn't guaranteed to remain the same across major versions of the .NET Framework. As a result, you shouldn't assume that the same seed will result in the same pseudo-random sequence in different versions of the .NET Framework.



该文档还包含其他部分中此建议的变体,例如在 Retrieve the same sequence of random values :

The following example uses 100100 as an arbitrary seed value to instantiate the Random object, displays 20 random floating-point values, and persists the seed value. It then restores the seed value, instantiates a new random number generator, and displays the same 20 random floating-point values. Note that the example may produce different sequences of random numbers if run on different versions of the .NET Framework.



和:

However, note that Random objects in processes running under different versions of the .NET Framework may return different series of random numbers even if they're instantiated with identical seed values.

关于c# - C#随机数生成器保证稳定吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62433503/

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