Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: rand / srand

by BrowserUk (Pope)
on Oct 24, 2006 at 07:51 UTC ( #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.

Comment on Re: rand / srand
Select or Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2015-11-28 19:20 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (743 votes), past polls