推荐答案
随机数生成器具有状态-实际上这是必要的功能.下一个随机"数字是前一个数字和种子/状态的函数.纯粹主义者称它们为伪随机数生成器.这些数字将通过统计检验以证明其随机性,但实际上不是随机的.
A random number generator has a state -- that's actually a necessary feature. The next "random" number is a function of the previous number and the seed/state. The purists call them pseudo-random number generators. The numbers will pass statistical tests for randomness, but aren't -- actually -- random.
随机值的序列是有限的,并且会重复.
The sequence of random values is finite and does repeat.
将随机数生成器想像为将一组数字混排,然后以随机顺序对其进行处理.种子用于混洗"数字.种子设定好后,数字序列就固定了,很难预测.有些种子会比其他种子早.
Think of a random number generator as shuffling a collection of numbers and then dealing them out in a random order. The seed is used to "shuffle" the numbers. Once the seed is set, the sequence of numbers is fixed and very hard to predict. Some seeds will repeat sooner than others.
大多数生成器的周期都足够长,以至于没人会注意到它在重复.一个48位随机数生成器在重复之前将产生数千亿个随机数-使用(AFAIK)任何32位种子值.
Most generators have period that is long enough that no one will notice it repeating. A 48-bit random number generator will produce several hundred billion random numbers before it repeats -- with (AFAIK) any 32-bit seed value.
当您给生成器一个种子并吐出值时,生成器将仅生成类似随机的值.如果更改种子,则与前一个种子生成的值相比,使用新种子值生成的数字可能不会随机出现-更改种子时所有赌注都关闭.所以不要.
A generator will only generate random-like values when you give it a single seed and let it spew values. If you change seeds, then numbers generated with the new seed value may not appear random when compared with values generated by the previous seed -- all bets are off when you change seeds. So don't.
一种合理的方法是拥有一个生成器,然后将数字出售"给您的各种客户.不要为创建和丢弃生成器而烦恼.不要惹种子.
A sound approach is to have one generator and "deal" the numbers around to your various clients. Don't mess with creating and discarding generators. Don't mess with changing seeds.
首先,请不要尝试编写自己的随机数生成器.大多数语言库中的内置生成器都非常好.尤其是使用32位以上的现代代码.
Above all, never try to write your own random number generator. The built-in generators in most language libraries are really good. Especially modern ones that use more than 32 bits.
某些Linux发行版具有/dev/random
和/dev/urandom
设备.您只需阅读一次即可为您的应用程序的随机数生成器添加种子.它们具有或多或少的随机值,但是它们通过收集来自随机系统事件的噪声"来工作.尽量少使用它们,因此每次使用之间会有很多随机事件.
Some Linux distros have a /dev/random
and /dev/urandom
device. You can read these once to seed your application's random number generator. These have more-or-less random values, but they work by "gathering noise" from random system events. Use them sparingly so there are lots of random events between uses.
这篇关于是否存在无状态随机数生成器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!