Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Dear monks

I need your help again.
For a tracking number, i generate random keys, with a length of 8 characters. The code i use is the following:

# srand(); # Generates collisions (no true random codes, also tried a +t the beginning of the code (not in the sub) # srand(time); # Generates collisions (no true random codes) sub GenCode { my $args = { AvailableChars => "ACDEFHIJKLMNPQRTUVWXYZ234679", CodeLength => 8, @_, }; # srand(); # Generates collisions (no true random codes, also tri +ed at the beginning of the code (not in the sub) # srand(time); # Generates collisions (no true random codes) srand (time ^ $$ ^ unpack "%L*", `ps axww | gzip`); #seems to work +, but is slow. my $rndcode = ''; my @AvailableCharsA = split "", $args->{AvailableChars}; for (my $j=0;$j<$args->{CodeLength};$j++) { $rndcode .= $AvailableCharsA[int(rand(@AvailableCharsA-1))]; } return $rndcode; }
The problem is the following:
If i use srand at the beginning of the module, it will create the same keys after some time again.
If i use srand(time), this also happens, so i've put it in the sub Gencode.
As the documentation of srand says, i should not do this, and next, the same keys were generated after some time again.
I searched Pelmonks, google, and came up with the above solution, using srand (time ^ $$ ^ unpack "%L*", `ps axww | gzip`);
This is 'slow', and i wonder why (mod) perl does not genterate true random codes, in the above code there should be enough space to generate lots of different keys without having collisions.

So the question is, how do i properly create random keys as described above

update
Thanks for all your help, i'll have to look into one of the solutions.
The thing (i think!) makes it difficult, is the fact that we preload all our modules into apache.
If the process is started and srand is called, i think the child process of apache uses the same seed (please correct me if i'm wrong here), thus generates the same random keys again.

"We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.

In reply to rand / srand by jbrugger

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-19 02:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found