Random Numbers in Perl

Kevin Buettner kev@primenet.com
Mon, 23 Oct 2000 23:21:27 -0700


On Oct 23, 10:22pm, foodog@uswest.net wrote:

>   I can't state that it's cryptographically sound*, but if I
> wanted a "comfortably random" number I'd do something like:
> read a pseudo-random number of bytes from /dev/random then
> pass the results through md5sum.  I'd be comfortable using
> rand() to determine how many bytes to get from /dev/random.

Take a look at the comments in drivers/char/random.c.  They are
very interesting.

>From my brief examination of this file, it appears to me that:

 - the fewer bytes that you fetch from /dev/random, the better
   off you are.  If you fetch too many bytes, the kernel will exhaust
   its entropy pool faster and you could be forced to rely on a
   psuedo-random generator.

 - the /dev/random driver appears to be already doing something
   roughly equivalent to an md5sum.  In fact, random.c contains the
   core of the MD5 algorithm.  However, it is not being used.
   In its place, something called a SHA hash is used on the entropy
   pool to generate random numbers.  If you prefer to use the MD5
   code, you can comment out the USE_SHA define.  I imagine that
   there's a good reason for using the SHA hash instead of MD5;
   perhaps using MD5 can expose the state of the entropy pool?  (I
   don't know for sure, but this seems likely from one of the
   comments.)

Kevin