c# - 针对相同版本的 .NET 框架的随机数种子

标签 c# random

如果我的应用程序以 .NET Framework 3.5 为目标,是否每个 CPU 都基于相同的种子返回相同的随机序列?我正在检查您是否得到与我相同的结果。我也希望我向其分​​发应用程序的每个人都能得到相同的结果。谢谢!

Random a = new Random(44448);
int i1 = a.Next(65, 90);
MessageBox.Show(i1.ToString());

最佳答案

对于特定的框架版本,您的程序在每次运行时都应该给出相同的结果,因为种子是固定的。

但它可以在不同版本的 .NET 框架上给出不同的结果。

例如,在 .NET 4.0 上,我得到 77。但是将代码放入 ideone (使用 Mono)给出 67

造成这种差异的原因是 Random 使用的精确算法不是规范的一部分。 documentation有关于算法的信息:

The current implementation of the Random class is based on a modified version of Donald E. Knuth's subtractive random number generator algorithm. For more information, see D. E. Knuth. "The Art of Computer Programming, volume 2: Seminumerical Algorithms". Addison-Wesley, Reading, MA, second edition, 1981.

...

The implementation of the random number generator in the Random class is not guaranteed to remain the same across major versions of the .NET Framework. As a result, your application code should not assume that the same seed will result in the same pseudo-random sequence in different versions of the .NET Framework.

强调我的。无法保证 future 的实现将使用相同的算法。

关于c# - 针对相同版本的 .NET 框架的随机数种子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11619074/

相关文章:

java - 给定范围内的随机 BigInteger 数组

c# - 使用终结点路由时不支持使用 'UseMvc' 配置 MVC

c++ - 如何编写一个可重复的伪随机数生成器?

c - 减去或添加到随机数生成器

c# - 了解文本编码(在 .Net 中)

java - 用随机数填充数组时 Math.random 的非常非随机因子

c - 是否可以调整计算机的随机函数?

c# - Visual Studio 2017 中的 Xamarin (Android) 单元测试

c# - Linq 或字符串过滤器

javascript - 从命名索引 JS 获取数组值