http://qs321.pair.com?node_id=580201

jbrugger has asked for the wisdom of the Perl Monks concerning the following question:

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.