.NET中Random类实现的质量如何? [英] What is the quality of Random class implementation in .NET?
问题描述
关于.NET Framework 4.6中Random
类的实现,我有两个问题(可用的代码此处):
I have two questions regarding implementation of Random
class in .NET Framework 4.6 (code available here):
-
在构造函数的末尾将
Seed
参数设置为1
的原理是什么?它似乎是从 C(第二版)中的数字配方复制粘贴而成的,这在一定程度上是有意义的,但是在C#中却没有.
What is the rationale for setting
Seed
argument to1
at the end of the constructor? It seems to be copy-pasted from Numerical Recipes in C (2nd Ed.) where it made some sense, but it doesn't have any in C#.
在书中(C中的数字食谱(第二版))直接指出,inextp
字段设置为值31
,原因是:
It is directly stated in the book (Numerical Recipes in C (2nd Ed.)) that inextp
field is set to value 31
because:
常数31是特殊的;见克努斯.
The constant 31 is special; see Knuth.
但是,在.NET实现中,此字段设置为值21
.为什么?除此细节外,其余代码似乎与本书中的代码密切相关.
However, in the .NET implementation this field is set to value 21
. Why? The rest of a code seems to closely follow the code from book except for this detail.
推荐答案
Regarding the intexp
issue, this is a bug, one which Microsoft has acknowledged and refused to fix due to backwards compatibility concerns.
实际上,您发现Random实现存在真正的问题. 我们已经在团队内部以及与一些合作伙伴进行了讨论,并得出结论,很遗憾,我们现在无法解决问题.原因是某些应用程序依赖于以下事实:当使用相同的种子初始化时,生成器会生成相同的伪随机序列.即使更改变得更好,一旦迁移到固定"版本,也会破坏做出此假设的应用程序.
Indeed, you have discovered a genuine problem with the Random implementation. We have discussed it within the team and with some of our partners and concluded that we unfortunately cannot fix the problem right now. The reason is that some applications rely on the fact that when initialised with the same seed, the generator produces the same pseudo random sequence. Even if the change is for the better, it will break the applications that made this assumption once they have migrated to the "fixed" version.
这篇关于.NET中Random类实现的质量如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!