为什么随机性不是那么随机性? [英] Why is random not so random?

查看:77
本文介绍了为什么随机性不是那么随机性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以提供有关现代编程语言(java,c#,python,javascript)如何应对随机性限制以及这些限制(例如基于时间的种子)起源的解释.即它们是否由底层操作系统和基于Intel的硬件强加.

Can someone provide an explanation as to how modern programming languages (java, c#, python, javascript) cope with the limitations of randomness and where those limitations (time-based seeds for example) originate. I.e if they are imposed by the underlying operating systems and intel based hardware.

基本上,我想了解为什么没有适当的硬件就没有真正的随机数之类的东西.

Basically i'd like to understand why there is no such thing as a truly random number without the appropriate hardware.

推荐答案

我将首先回答您问题的第二部分:

I'm going to answer the second part of your question first:

基本上,我想理解为什么没有适当的硬件就没有真正的随机数之类的东西.

Basically I'd like to understand why there is no such thing as a truly random number without the appropriate hardware.

如果没有特殊硬件,则无法在计算机上生成真正的随机数,因为计算机是确定性计算机.这意味着在给定一些初始状态和要执行的操作后,您可以准确预测机器的发展方式.例如,如果您知道,在某种假设的体系结构上,该寄存器%d0包含24,而该寄存器%d1包含42,并且您知道指令流中的下一条指令是add %d0 %d1 %d2,则可以知道执行该指令后,%d2将包含66.在更高级的语言中,您知道编写x = 1; y = 2; z = x + y 会导致z确实是3.

You can't generate truly random numbers on a computer without special hardware because computers are deterministic machines. What this means is that, given some initial state and an operation to perform, you can predict exactly how the machine will evolve. For instance, if you know that, on some hypothetical architecture, that register %d0 contains 24 and register %d1 contains 42, and you know that the next instruction in the instruction stream is add %d0 %d1 %d2, you then know that, after that instruction is executed, %d2 will contain 66. In a higher-level language, you know that writing x = 1; y = 2; z = x + y will result in z being 3 with certainty.

这很有道理;我们不想知道加法会做什么,我们希望它是 add .但是,这与生成真正的随机数不兼容.要使数字真正是随机的,无论您知道什么,都绝对不能无法预测.某些量子力学过程精确地具有这种行为,而其他自然过程则非常接近于随机,以至于出于所有实际目的,它们都是随机的(例如,如果它们看起来是随机的,并且预测它们将需要知道大气中每个分子的状态) ).但是,计算机无法做到这一点,因为拥有计算机的整个要点是要有一台确定性地执行代码的机器.您需要能够预测运行程序时会发生什么,否则有什么意义呢?

This makes sense; we don't want to wonder what an addition will do, we want it to add. However, this is incompatible with generating truly random numbers. For a number to be truly random, there needs to be absolutely no way to predict it, no matter what you know. Certain quantum-mechanical processes have this behavior precisely, and other natural processes are close enough to random that, for all practical purposes, they are (for instance, if they look random and predicting them would require knowing the state of every molecule in the atmosphere). However, computers cannot do this, because the whole point of having a computer is to have a machine which deterministically executes code. You need to be able to predict what will happen when you run programs, else what's the point?

在对 Milan Ramaiya的答案的评论中,您说

我同意[yo] u,但仍然错过了最重要的事情-为什么计算机不能生成具有预定输入的随机数?

I agree with [yo]u but still missing the most important thing - why cant computers produce a random number with pre-determined input?

答案直接来自真正随机数的定义.由于真正的随机数需要完全不可预测,因此它从不依赖于确定性输入.如果您有一个采用预定输入的算法并使用它来生成伪随机数,则只要知道输入和算法,就可以随意重复此过程.

The answer falls out directly from the definition of a truly random number. Since a truly random number needs to be completely unpredictable, it can never depend on deterministic input. If you have an algorithm which takes pre-determined input and uses it to produce a pseudo-random number, you can duplicate this process at will just as long as you know the input and algorithm.

您还问了

有人可以提供关于现代编程语言如何应对随机性局限性以及这些局限性在何处起源的解释.

Can someone provide an explanation as to how modern programming languages … cope with the limitations of randomness and where those limitations … originate.

好吧,如上所述,这些限制是我们的语言和机器的确定性设计所固有的,存在这些限制是有充分的理由的(以便可以使用所述语言和机器:-)).假设您没有呼唤确实可以访问真正随机数的东西(例如在存在它的系统上为/dev/random),则采用的方法是使用伪随机数生成器.这些算法旨在产生统计上随机的输出序列-从形式上讲,看起来变幻莫测.我没有足够的统计数据来解释或理解其详细信息,但我相信,这是因为您可以运行某些数值测试来告诉您数据在某种程度上预测自己(在某种程度上来说是宽松的)以及类似的事情.但是,重要的一点是,尽管序列是确定性的,但它看起来是随机的".对于许多目的来说,这就足够了!有时它具有优点:例如,如果您要测试代码,能够指定种子并始终让其接收相同的伪随机数序列会很不错.

Well, as mentioned above, the limitations are inherent to the deterministic design of our languages and machines, which are there for good reasons (so that said languages and machines are usable :-) ). Assuming you aren't calling out to something which does have access to truly random numbers (such as /dev/random on systems where it exists), the approach taken is to use a pseudo-random number generator. These algorithms are designed to produce a statistically random output sequence—one which, in a formal sense, looks unpredictable. I don't know enough statistics to explain or understand the details of this, but I believe the idea is that there are certain numeric tests you can run to tell how well your data predicts itself (in some loose sense) and things like that. However, the important point is that, while the sequence is deterministic, it "looks random". For many purposes, this is enough! And sometimes it has advantages: if you want to test code, for instance, it can be nice to be able to specify a seed and always have it receive the same sequence of pseudo-random numbers.

总而言之,对您的问题的总体回答是:由于我们希望能够预测计算机的功能,因此它们无法生成不可预测的数字(没有特殊的硬件).编程语言通常不会受此影响,因为在大多数情况下伪随机数生成器就足够了.

In summary, the overall answer to your question is this: Because we want to be able to predict what computers do, they can't generate unpredictable numbers (without special hardware). Programming languages aren't generally too impacted by this, because pseudo-random number generators are sufficient for most cases.

这篇关于为什么随机性不是那么随机性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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