是否存在无状态随机数生成器? [英] Do stateless random number generators exist?

查看:140
本文介绍了是否存在无状态随机数生成器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用单个随机数生成器(RNG)生成多个数字与为每个生成器生成一个数字并将其丢弃之间有区别吗?两种实现方式都会产生相等随机的数字吗?普通RNG和安全RNG之间有区别吗?

Is there a difference between generating multiple numbers using a single random number generator (RNG) versus generating one number per generator and discarding it? Do both implementations generate numbers which are equally random? Is there a difference between the normal RNGs and the secure RNGs for this?

我有一个Web应用程序,应该可以代表客户生成随机数列表.也就是说,从每个客户的角度来看,这些数字应该看起来是随机的.这是否意味着我需要在每个客户端会话中保留一个单独的随机RNG?或者我可以在所有会话中共享一个RNG吗?还是可以根据每个请求创建和丢弃RNG?

I have a web application that is supposed to generate a list of random numbers on behalf of clients. That is, the numbers should appear to be random from each client's point of view. Does this mean I need retain a separate random RNG per client session? Or can I share a single RNG across all sessions? Or can I create and discard a RNG on a per-request basis?

更新:此问题与推荐答案

随机数生成器具有状态-实际上这是必要的功能.下一个随机"数字是前一个数字和种子/状态的函数.纯粹主义者称它们为伪随机数生成器.这些数字将通过统计检验以证明其随机性,但实际上不是随机的.

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屋!

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