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

标签 c# random

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

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/

相关文章:

c# - OpenXML 标记搜索

c# - MVC 5 C#将错误报告回数据库

java - 如何在冒泡排序中对随机数进行排序

c - C 中的 Rand() 函数用于随机 ASCII - 字符(仅限字母和数字)

c++ - 井字游戏随机 AI

c# - 如何根据C#中矩形数组中的特定值检查特定单元格的周围单元格

c# - 辅助功能 : Managed vs Unmanaged code

c# - LINQ 多列表连接

arrays - Julia - 随机数组

python - random.random() 在 for 循环中产生相同的结果