Why is it hard for a program to generate random nu

2019-01-23 03:22发布

My kids asked me this question and I couldn't really give a concise, understandable explanation.

So I'm hoping someone on SO can.

标签: random
26条回答
干净又极端
2楼-- · 2019-01-23 03:33

Because given any input, an algorithm produces the exact same output every single time. And you can't just provide a "random" input, because you're trying to generate the random number in the first place.

查看更多
不美不萌又怎样
3楼-- · 2019-01-23 03:37

Algorithms to generate random numbers are inevitably deterministic. They take a small random seed, and use it to obtain a long string of pseudo-random digits.

It's very difficult to do this without introducing subtle patterns into the data. A string of digits can look perfectly random but have repeated patterns which make the distribution innappropriate for applications where randomness is required.

查看更多
Lonely孤独者°
4楼-- · 2019-01-23 03:37

To make the computer generate a random number, the computer has to have a source of randomness to start with.

It has to be feeded a seed that can't be expected or calculated by just looking at the seed, if the seed comes from a clock then it can be predicted or calculated by knowing the time, if the seed comes from like filming a lavalamp and get numbers from the picture stream then it's harder to just look at the seed to know what next number will be.

The computer does not have an built in lava lamp to generate that randomness, thats whats make it hard, we have to substitute real randomness with some input that exists in the computer, maybe by logging passing tcpip-packets or other things, but its not many ways to get that randomness sources in.

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-23 03:39

It is hard because given the same sets of inputs and conditions, a program will produce the same result everytime. This by definition is not random.

查看更多
叛逆
6楼-- · 2019-01-23 03:39

Actually, on most modern computers it's not hard to produce numbers that are "random enough" for most purposes. As others have noted, the critical thing is having a source of randomness. You can't just write a program that will produce randomness algorithmically, but you can observe randomness in the various activities of most computers of reasonable complexity, i.e., the ones we typically think of when writing programs. One such source is timing data of interrupts from various system devices.

At one time many computers had no way to get at this data and could only offer pseudorandomness, that is, a random, but repeatable distribution of numbers based on a particular seed. For many purposes this is sufficient -- choosing a different seed each time results in good enough randomness. For other purposes, such as encryption, this isn't strong enough and you need some randomness to start with that isn't repeatable or predictable. Today, most computers (with the exception of embedded devices, perhaps) are sophisticated enough to have a source of randomness that can generate encryption-strength random numbers. For instance, Linux has /dev/random and the .NET framework supports the cryptographically strong RandomNumberGenerator class which has a number of implementations.

查看更多
Luminary・发光体
7楼-- · 2019-01-23 03:40

Ask them to devise a step-by-step method to generate a random number.

And don't accept "pick a number from 1 to 10" as an answer ;)

Trying out a problem should illustrate the difficulty of having to generate random numbers from a set of instructions, just like what computers actually have to do.

查看更多
登录 后发表回答