It drives me crazy! I have a script that needs to create a random identifier so that the file can be renamed. Here is the test I have:
for (my $i = 0; $i <= 100; $i++) {
my $test = rand_id();
print "FOO: $test\n";
}
sub rand_id {
use String::Random qw(random_regex random_string);
my $rand = random_regex("[a-zA-Z0-9]"x20);
return $rand;
}
This works the way I hoped:
root@admin:# perl test.cgi
FOO: P2kyuotdlk04gcafvoze
FOO: ZGC44tXfGFaiXeHsLZdn
FOO: fydWFp1PW6iGYFaOfgvx
FOO: xG4SPx2gLGPVeMJOupZ9
FOO: 6A2uD9hCF90VP7ybKjiA
FOO: wT4fG8ogmV37Mkljs0gE
FOO: 6QttcmlNjO1o9jCVht3g
FOO: 9bAYYDd2NIjBWgAhsl3t
FOO: hrU04kHvxu0JJYPHv6Jk
FOO: 9EVL9GqGdWWZhDam6dc9
FOO: F0zruszqvmMOUumlO4Q1
FOO: KA9jOof9iSTxpDOWUMBl
FOO: phicMiogMhZIcPZiXvj8
etc
So, all is well (without repetitions).
I then have almost the same code (only not in the loop) in my script, but when I continue to restart from the browser, I get:
NEW: 0ab3K60sJSpHrvDVCKcR
NEW: 0ab3K60sJSpHrvDVCKcR
NEW: 0ab3K60sJSpHrvDVCKcR
NEW: 0ab3K60sJSpHrvDVCKcR
NEW: 0ab3K60sJSpHrvDVCKcR
NEW: EyiYp5D6d8CL3vzozYFZ
NEW: EyiYp5D6d8CL3vzozYFZ
NEW: EyiYp5D6d8CL3vzozYFZ
NEW: EyiYp5D6d8CL3vzozYFZ
NEW: EyiYp5D6d8CL3vzozYFZ
NEW: A0KUBiNSDcxyX7JQzBsk
NEW: A0KUBiNSDcxyX7JQzBsk
NEW: A0KUBiNSDcxyX7JQzBsk
NEW: A0KUBiNSDcxyX7JQzBsk
NEW: A0KUBiNSDcxyX7JQzBsk
NEW: 7wU3RgK1Ho16rEjkGSsB
NEW: 7wU3RgK1Ho16rEjkGSsB
NEW: 7wU3RgK1Ho16rEjkGSsB
As you can see, there are LOTS of duplicated "random" identifiers. What am I doing wrong?
The module I'm using is http://search.cpan.org/~steve/String-Random-0.20/Random.pm , but I get the same problem when you do something like:
my $rand = Digest::MD5::md5_base64( rand );
source
share