哪个更随机:由人工生成还是由软件生成的数字? [英] Which is more random: human- or software-generated number?

查看:63
本文介绍了哪个更随机:由人工生成还是由软件生成的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

扔硬币以获得随机比特吗?
还是掷骰子以获取1到6之间的随机整数?
还是从经过打乱的牌堆中拿一张牌来获得1到52之间的数字?
.
.
.
还是可以像我们一样思考或拥有像我们一样的智慧?

Does it toss a coin to get random bit?
Or throw a die to get a random integer from 1 to 6?
Or take a card from a shuffled deck to get a number from 1 to 52?
.
.
.
Or can it think like us or have intelligence like us?

显然,以上示例不是生成随机数据的方法.

Obviously the above examples can't be ways of generating random data.

然后,软件库如何在给定范围内生成random数字? 哪个更随机:是由人还是软件生成的?

Then how do software libraries generate random numbers on a given range? Which is more random: human- or software-generated?

推荐答案

(注意:这通常是关于计算中的随机[和伪随机]数字及其用途.)

通过确定性过程永远不能拥有真正的随机数,这就是为什么计算机不太适合生成随机数的原因(因为CPU只能以确定性方式翻转位).大多数语言,框架和库都使用所谓的 -随机数生成器(PRNG).它们采用 seed ,它是一种初始状态向量,可以是单个数字或数字数组,并从那里生成一系列看似随机的值.结果通常满足一定的统计指标,但并非完全随机,因为同一种子将产生完全相同的序列.

You can never have true random numbers with a deterministic process, which is why computers are fairly ill-suited to generate them (as the CPU can only flip bits in a deterministic manner). Most languages, frameworks and libraries use so-called Pseudo-random number generators (PRNG). Those take a seed which is sort of an initial state vector which can be a single number or an array of numbers and generate a sequence of seemingly-random values from there. The results usually satisfy certain statistical measures, but are not totally random, as the same seed will yield the exact same sequence.

最简单的PRNG之一就是所谓的线性同余生成器(LCG).它只有一个数字作为状态(最初是种子).然后,对于每个连续的返回值,公式将像这样循环:

One of the easiest PRNGs is the so-called Linear Congruential Generator (LCG). It just has a single number as state (which is initially the seed). Then for each successive return value the formula loops like this:

其中 a b c 是生成器的常量. c 通常是2的幂,例如2 32 ,只是因为它易于实现(自动完成)且速度很快.但是,很难找到 a b 的值.作为最简单的示例,当使用 a = 2和 b = 0时,您会看到结果值永远不会是奇数.这限制了发生器可以非常严格地产生的值的范围.通常,LCG是一个非常古老的概念,长期以来被更好的生成器所取代,因此除非在极其有限的环境中使用,否则不要使用它们(尽管即使嵌入式系统也可以毫无问题地处理更好的生成器)– WELL生成器对于不希望担心其伪随机数属性的人来说通常要好得多.

where a, b and c are constants for the generator. c is usually a power of two, such as 232 simply because it's easy to implement (done automatically) and fast. Finding good values for a and b is hard, however. As a most trivial example, when using a = 2 and b = 0, you can see that the resulting values can never be odd. This limits the range of values the generator can yield quite severely. Generally, LCGs are a very old concept and long supplanted by much better generators, so don't use them, except in extremely limited environments (although even embedded systems can handle better generators without problem, usually) – MT19937 or its generalization, the WELL generators are usually much better for people who simply don't want to worry about the properties of their pseudo-random numbers.

PRNG的一个主要应用是仿真.由于PRNG可以估算或保证统计特性,并且由于种子的性质,可以精确地重复进行实验,因此PRNG在这里可以做得很好.假设您要发表论文,并希望其他人复制您的结果.使用硬件RNG(如下所述),除了包含您使用的每个单个随机数之外,您别无选择.对于可以轻松使用数十亿个甚至更多个数字的蒙特卡洛模拟,这是不可行的.

One major application of PRNGs is simulation. Since PRNGs can give an estimate or guarantee of statistical properties and an experiment can be repeated exactly due to the nature of the seed they do quite well here. Imagine you're publishing a paper and want other people to replicate your results. With a hardware RNG (se below) you have no other option than including every single random number you used. For Monte-Carlo simulations which can easily use a few billion numbers or more this is ... not quite feasible.

然后有用于密码应用程序的随机数生成器,例如用于保护您的SSL连接.例如Windows的 CryptGenRandom 或Unix的几年前Debian在OpenSSL中出错了.

Then there are random number generators for cryptographic applications, e.g. for securing your SSL connection. Examples here are Windows' CryptGenRandom or Unix's /dev/urandom. Those often are also a PRNG, however they use a so-called "entropy pool" for seeding which contains unpredictable values. The main point here is to generate unpredictable sequences, even though the same seed will still yield the same sequence. To minimize the effect of an attacker guessing the sequence they need to be re-seeded regularly. The entropy pool is gathered from various points in the system: events, such as input, network activity, etc. Sometimes it's initialized also with memory locations throughout the system assumed to contain garbage. However if it's done, care must be taken to ensure that the entropy pool really contains unpredictable stuff. Something that Debian got wrong in OpenSSL a few years ago.

您还可以直接使用熵池来获取随机数(例如Linux的 /dev/random ;对于/dev/random,FreeBSD使用与/dev/urandom相同的算法),但是不会从中得到太多,一旦它为空,则需要一段时间才能补充.这就是为什么上面提到的算法通常用于将很小的熵扩展到更大的体积的原因.

You can also use the entropy pool directly to get random numbers, (e.g. Linux's /dev/random; FreeBSD instead uses the same algorithm for /dev/random as for /dev/urandom), but you don't get too much out of it and once it's empty it takes a while to replenish. That's why the algorithms mentioned above are usually used to extend what little entropy there is to larger volumes.

然后有基于硬件的随机数生成器,它们使用不可预测的自然过程,例如放射性衰变或电线中的电噪声.那些是最苛刻的应用程序,需要许多真正的"随机数,并且通常每秒能够产生数百MiB的随机性(好吧,该数据点已经使用了几年,但是我怀疑这样做可以做很多)现在更快).

Then there are hardware-based random number generators, which use unpredictable natural processes, such as radioactive decay or electrical noise in a wire. Those are for the most demanding of applications requiring many "truly" random numbers and are able to generate a few hundred MiB of randomness per second, usually (ok, that data point is a few years old, but I doubt it can be done much faster by now).

您可以编写一个程序来模拟这样的事情,该程序是从带镜头盖的网络摄像头拍摄图像(然后仅保留噪点),或者从没有实际输入的情况下从音频输入获取图像.那些对于一些黑客来说是很好的,但是通常不会产生 good 随机数,因为它们是有偏见的,即在比特流中零和一个不是以相同的频率表示的(或者更进一步,序列00011011的生成频率不同...对于较大的序列也可以执行此操作).因此,实际硬件RNG的一部分将用于确保结果值满足某些统计分布属性.

You can emulate such things by writing a program taking images from a webcam with lens cap on (only noise remains, then) or from audio input when no actual input is present. Those are fine for a little hacking, but usually will not generate good random numbers, as they are biased, i.e. in the bit stream zeroes and ones are not represented with the same frequency (or, going further, the sequences 00, 01, 10 and 11 are not generated with the same frequency ... you can do this for larger sequences as well). So a part of an actual hardware RNG goes into making sure that the resulting values satisfy certain statistical distribution properties.

有些人实际上扔了骰子以得到随机掷骰子,甚至非常糟糕的随机数生成器.

Some people actually throw dice to get random dice rolls or even take this into overdrive. And humans make very bad random number generators.

这篇关于哪个更随机:由人工生成还是由软件生成的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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