重复执行Random().Next()流需要多长时间? [英] How long does the stream of Random().Next() take until it repeats?

查看:120
本文介绍了重复执行Random().Next()流需要多长时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑.NET Random流:

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

重复需要多长时间?

推荐答案

根据文档:

从有限的概率中选择伪随机数 一组数字.选择的数字是 不是完全随机的,因为 确定的数学算法是 用于选择它们,但是它们是 足够随机以实用 目的.目前的实施 的随机类别基于唐纳德(Donald) E. Knuth的减法随机数 生成器算法.欲了解更多 有关信息,请参阅D. E. Knuth. 艺术 计算机编程》,第2卷: 半数值算法". 艾迪生·韦斯利(Addison-Wesley),马萨诸塞州雷丁,第二名 版,1981年.

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)mod 1. 有关k和j的可能值的表,请参见Knuth.我们选择k = 63,j =31.此生成器很有趣,因为:

The subtractive generator (Knuth, Vol 2) Xf,n = (Xf,n-k - Xf,n-j) mod 1. See Knuth for a table of possible values of k and j. We choose k = 63, j = 31. This generator is interesting because:

  • 时间长.此序列中最低有效位的周期为2 k -1.实际时间比这更长.
  • 在一些轻微的限制下,涉及的浮点运算是精确的!
  • It has a long period. The period of the least significant bit in this sequence is 2k-1. The actual period is much longer than this.
  • With some mild restrictions, the floating point arithmetic involved is exact!

当X为以下形式时,第二个属性成立 247 (0 l< 247) 单精度算法在Crays(48位尾数)上是精确的,而在IEEE兼容计算机上则是双精度算法.

The second property holds when X is of the form l 247 (0 � l < 247) Single-precision arithmetic is exact on the Crays (48-bit mantissa) and as is double-precision arithmetic on IEEE-compliant machines.

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

This allows the basic random number sequence to be generated by the Fortran code

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

在实践中,随机数会根据需要分批生成,并存储在用作循环缓冲区的数组中.

In practice random numbers are generated in batches as needed and stored in an array which acts as a circular buffer.

上述算法的周期取决于种子值-您可以找到更多详细信息在这里.

The algorithm mentioned has a period that depends on the seed value - you can find more details here.

这篇关于重复执行Random().Next()流需要多长时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆