Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: rand / srand

by BrowserUk (Patriarch)
on Oct 24, 2006 at 07:51 UTC ( [id://580210]=note: print w/replies, xml ) Need Help??


in reply to rand / srand

Don't use srand. It's purpose is to make thing less random (more determanistic), not more so.

Update: For this purpose, random integers in the range 0 .. 35, even the paltry 15-bit standard rand on my system is perfectly up to this task.

Try this. This implementation of GenCode() and test program happily produces 10e6 unique random strings, from the 2.8 trillion possibles, before exhausting my memory and trapping.

#! perl -slw use strict; $|++; sub rndStr{ join'', @_[ map{ rand @_ } 1 .. shift ] } sub GenCode{ return rndStr 8, 'A'..'Z', 0..9 } my %hits; until( exists $hits{ $_ = GenCode() } ){ keys( %hits ) % 10_000 or printf "\r%d\t", scalar keys %hits; ++$hits{ $_ } ; } printf "Collision after %d generations\n", scalar keys %hits;

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-03-29 12:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found