Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2014-10-25 15:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (145 votes), past polls