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;
|
|---|