Chapter 26. Randomness
Jon Orwant
One of the common questions on comp.lang.perl.misc is “How come my random numbers aren’t random?” Usually, a simple prescription to use srand
is sufficient. But the simplicity of these answers belies just how hard it is to have a computer generate good random numbers. In fact, it’s downright impossible.
While no source provides completely random numbers, some sources are more random than others. The PGP encryption package, for instance, has you type aimlessly at your keyboard, using the intervals between keystrokes as a source of random numbers. It measures tiny time differences, making it a good source of random numbers unless you possess a truly robotic sense of rhythym, in which case you probably have career options that don’t involve the use of contraband cryptosystems.
The measurement of keystrokes is slow and intrusive, so some computers rely on rapidly changing areas of computer memory for their random numbers, or network packet arrival times. Others amplify ambient noise collected from disconnected audio or video inputs. Still others calculate disk seek times; minute variations in speed are caused by air turbulence, which is pretty hard to predict.
Computers that take their randomness seriously have physical hardware dedicated to random number generation: unstable free-running oscillators, a source of radioactive decay, or thermal noise from an amplified diode. That’s about as random as it gets.
But the vast majority of computers generate random ...
Get Computer Science & Perl Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.