如果我的应用程序以 .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/