Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Comment on

( #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

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

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (5)
    As of 2017-12-17 21:46 GMT
    Find Nodes?
      Voting Booth?
      What programming language do you hate the most?

      Results (466 votes). Check out past polls.