r/AskComputerScience • u/[deleted] • Apr 10 '20
How does a computer generate a random number?
39
u/turtle_dragonfly Apr 10 '20 edited Apr 10 '20
First, you need to distinguish between "truly random" and "pseudorandom". People sometimes use one to refer to the other.
If you mean "truly random", then that generally has to come from outside the computer. Your computer maintains something called an "entropy pool" which gets filled with random information from the environment, such as the arrival times of network packets, input timings of your mouse and keyboard, etc.
Some computers even have special devices plugged in for generating random numbers. This could be from a web camera, a Geiger counter pointed at a radioactive isotope, etc.
This is a silly example, but you get the idea: LavaRand
The downside of true random numbers is that they are very slow to produce, relative to the CPU.
But most of the "random" numbers you use are not what I talked about above, but are "pseudorandom" numbers. You might generate a "seed" number for them from an expensive true random source, but after that, the rest of the numbers generated are done in a deterministic fashion.
The Middle Squared method is not very good, but it's easy to understand and you can get the idea from it.
There's a lot written on the overall topic of PRNGs; a good starting point is Wikipedia: Pseudorandom Number Generator
Edit: there is also some nuance between "random" and "hard to predict". The former guarantees the latter, but the reverse is not true. Generally, for a PRNG seed, you merely want the latter. So, you might see people use things like: (1) the current time or (2) the current process's PID. These are not random, but they are generally hard(ish) to predict.
1
u/ConorDrew Apr 10 '20
There is this video on procedural generation in video games and they mention the two types of number generation link
1
2
u/coterminous_regret Apr 10 '20
Others here have done a good job of outlining the difference between RNG and PRNG. From a practical standpoint there are a few interesting algorithms that you will find commonly used for PRNG
The first being mersenne twister
Linear feedback shift registers which exhibit a number of interesting properties and can be useful for a number of things
2
u/SpiderAlpha33 Apr 10 '20
It most likely gets some non-standard data from the environment such as air pressure, cpu cycles, clock speeds and signals which are not deterministic in nature. True randomness works like this.
2
Apr 11 '20
Check out Donald Knuth's The Art of Computer Programming chapter 3 (in volume 2). If anything skim to the formulas. It does a good job laying out some starting algorithms.
14
Apr 10 '20
[removed] — view removed comment
16
u/jeffbell Apr 10 '20
My favorite hardware random number generator is a camera pointed at a wall of lava lamps. The output is a hash of the latest frame. Sun had a patent application.
https://patents.google.com/patent/US5732138A/en
By remarkable coincidence the patent application expires today! 4/10. Here's an opportunity.
5
u/roman_fyseek Apr 10 '20
They told us in Army radio school that our crypto keys were generated by continuous sampling of audio at a busy airfield. I don't know if there's any truth to it, but it sounded about right.
8
5
6
u/[deleted] Apr 11 '20 edited Apr 11 '20
There is a tiny little dwarf inside of every CPU, he is very very small, you can only see him with a microscope and a special filter that you can only get from Southern Algeria.
Anyway, every time you tell your computer to generate a random number, this little man does exactly two back-flips, one squat and half a push-up, then he stands up, and points to the nearest Nitrogen atom to his right foot. The last 4 digits of the GPS coordinate of this nitrogen atom will be your random number. If you need a longer number he multiplies the 4 digit number by his birth year; 1912.