gpt4 book ai didi

c# - Random().Next() 流需要多长时间才能重复?

转载 作者:太空狗 更新时间:2023-10-29 17:44:06 41 4
gpt4 key购买 nike

考虑 .NET Random 流:

var r = new Random(); 
while (true)
{
r.Next();
}

重复需要多长时间?

最佳答案

根据文档:

Pseudo-random numbers are chosen with equal probability from a finite set of numbers. The chosen numbers are not completely random because a definite mathematical algorithm is used to select them, but they are sufficiently random for practical purposes. The current implementation of the Random class is based on 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.

减法生成器(Knuth,第 2 卷)Xf,n = (Xf,n-k - Xf,n-j) 模 1。有关 k 和 j 的可能值的表,请参阅 Knuth。我们选择 k = 63,j = 31。这个生成器很有趣,因为:

  • 它的周期很长。该序列中最低有效位的周期为2k-1。实际周期比这长得多。
  • 在一些温和的限制下,所涉及的浮点运算是精确的!

当 X 的形式为 升 247 (0 � l < 247)单精度算法在 Crays(48 位尾数)上是精确的, double 算法在符合 IEEE 的机器上也是如此。

这允许通过 Fortran 代码生成基本的随机数序列

  x(n) = x(n-k) - x(n-j)
if (x(n) < 0.0) x(n) = 1.0 + x(n)

在实践中,随机数是根据需要分批生成的,并存储在一个充当循环缓冲区的数组中。

提到的算法有一个周期取决于种子值 - 你可以找到 more details here .

关于c# - Random().Next() 流需要多长时间才能重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2050392/

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