Initializing Perl's rand

Doug.Palmer at csiro.au Doug.Palmer at csiro.au
Tue Jun 25 12:51:24 EST 2002


> If time() has one-second resolution, then hash(time()) will also have
> one-second resolution.

This is true. 

However, from pp.c:

S_seed(pTHX)
{
    /*
     * This is really just a quick hack which grabs various garbage
     * values.  It really should be a real hash algorithm which
     * spreads the effect of every input bit onto every output bit,
     * if someone who knows about such things would bother to write it.
     * Might be a good idea to add that function to CORE as well.
     * No numbers below come from careful analysis or anything here,
     * except they are primes and SEED_C1 > 1E6 to get a full-width
     * value from (tv_sec * SEED_C1 + tv_usec).  The multipliers should
     * probably be bigger too.
     */

The seed is simply built from linear combinations of the parameters
available, multiplied by various prime numbers. I suspect that a lot of the
garbage values (time, PID, stack pointer position, address of the time
struct) all have similar values when running a specific perl program. What
this boils down to is that a change of one second will only change one bit
of the seed 50% of the time.

SHA has the desired property of spreading the effect of input bits, meaning
that a second difference can have a lot of difference on the seed.




More information about the linux mailing list