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

标签 c# random cycle

考虑 .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/

相关文章:

ruby - 有向无环图中的循环检测更快?

c# - asp.net 工具提示对于里面的文本来说显得很大

c# - unity : Remove The selected object from random.范围

c# - 通过 Entity Framework 执行 SProc 时未获取毫秒数据

c - 尝试随机生成一个字符串以便执行条件

php - 重新生成 "random"MySQL 查询

c# - 如何避免用不同的方法重写相同的代码

php - 随机数、列和行。 PHP

java - 为什么 Java toString() 在间接循环上无限循环?

c# - 这个 C# 结构有什么问题?