Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
XP is just a number
 
PerlMonks  

Perl's auto srand() RE: a random sort of list

by tye (Cardinal)
on Sep 08, 2000 at 23:29 UTC ( #31658=note: print w/ replies, xml ) Need Help??


in reply to RE: RE (tilly) 1: a random sort of list
in thread "a random sort of list"

Actually, modern Perl (since 5.004) does much better than srand(time()). The probable seed looks something like:

srand 1000003*time() + 3*$usec + 269*$$ + 73819*${undef} + 26107*\$x
where ${undef} is whatever integer is left on the stack and \$x is a pointer into the stack. Note that the above Perl code doesn't actually work; it is just an approximation of what the C code inside Perl is doing.

On systems with /dev/urandom, that is just used instead, which is pretty good. Use /dev/random if you have it, though you may have to wait for enough entropy to gather. But back to the case of systems without /dev/*random...

Although the code is described as a "quick hack" (because it doesn't do some fancy summing but just multiplies and adds), it would be hard to do much better portably from within a Perl script.

But this still isn't enough for cryptographic uses. Repeated runs of the same script might well yield the same values for the "what is left on the stack" and the "address into the stack" while the other values can be predicted to a certain extent.

So if you come up with something that seems really hard to predict, just add it into Perl's seed rather than replacing it. In other words:

srand( fancyseed() );
is probably not nearly as good of an idea as, for example:
srand( rand(~0) ^ fancyseed() );
Suggestions for better ways to add randomness in are welcome.

The documentation on srand() in perlfunc.pod is also worth reading.

        - tye (but my friends call me "Tye")


Comment on Perl's auto srand() RE: a random sort of list
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://31658]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2014-04-20 19:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (487 votes), past polls