7.1
Random Number Generators
A random number
generator
is a
subroutine you can call. For
most practical purposes, you
needn't worry about what's inside it. The values you get
for random numbers on the computer differ somewhat from the values of
real-world random events as measured, for example, by detecting
nuclear decay events. Some computers actually have devices such as
geiger counters attached so as to have a source of truly random
events. But I'd be willing to bet your computer doesn't.
What you have in place of a geiger counter, is an algorithm called a
random number generator.
The numbers that are output by random number generators are not
really random; they are thus called pseudo-random
numbers. A random number generator, being an
algorithm, is predictable. A random number generator needs a
seed, an input you can change to get a
different series of (pseudo-)random numbers.
The numbers from a random number generator give an even distribution
of values. This is one of the most important characteristics of
randomness and largely justifies the use of these algorithms where
some amount of random behavior is desired.
The other "take-home message" about random number
generators is that the seed you start them up with should itself be
selected randomly. If you seed with the same number every time,
you'll get the same sequence of "random numbers"
every time as well. (Not very random!) Try to pick a seed that has
some randomness in it, such as a number calculated from some computer
event that changes haphazardly over time.[1]
In the examples that follow, I use a simple method for seed picking
that's okay for most purposes. If you use random numbers for
data encryption with critical privacy issues (such as patient
records), you should read further into the Perl documentation about
the several advanced options Perl provides for random number
generation. In this book, I use a Perl method that is good enough for
most purposes.