How do I get random #s?

Jim Adcock jima at hplsla.HP.COM
Wed Feb 15 04:21:22 AEST 1989


> geez...my favorite random number generator on a similar hardware vein is
>  use the noise off of a cheap transistor, fed into a counter register...

I would not expect this to be a good random number generator --
unless the time between successive readings of the counter is itself a
pretty random amount of time, or the counter goes through a very large
number of complete cycles between subsequent readings.  Otherwise you
can expect a fair amount of correlation between subsequent numbers.

Most trivial "hardware" solutions fail to give very random numbers
for reasons that are very similar to reasons trivial "software"
solutions fail to give very random numbers.

XOR'ing a "hardware [noise-diode-like]" solution with a "software"
solution can give a better overall solution if both are very good
"random" number generators to begin with,  whose individual bits
are all "zero mean" [equal probability of ones and zeros.]

Its extremely hard to make good random number generators using
a purely hardware approach, since its hard to make analog noise
that doesn't have any 60Hz, 120Hz, 180Hz, chopper power supply
frequencies, feedthru from the electronics of the computer
you're attached to, etc.

...And if you're using a custom designed "hardware" [and/or 
software] solution you still need to "prove" that your
"random numbers" pass all the common simple tests of
randomness.  Which they probably won't.

...And if you use a combined hardware/software solution, then
you lose the repeatability that setting a seed in a purely
"software" solution gives you.

For example, we frequently use random numbers to test software,
using the random numbers to choose from the various commands that
our software implements, with randomly selected parameters,
in random order.  Its very nice to use this technique to 
find software bugs, fix the bugs, then re-run the random command
generator software with the same seed as a regression test.
Can't do this with a combined hardware/software scheme.

While, for many trivial applications, like playing games,
one may not care too much how good your random numbers
are, but in many cases one needs to be able to assume
the numbers really are "random" in order to be able to
make reasonable statistical assumptions about whatever
it is one's testing.

In summary, there's lots of simple, trivial, fast, but lousy
ways to make random numbers in either hardware and/or software.
Test what you would use.  Caveat Emptor.



More information about the Comp.lang.c mailing list