jeudi 19 octobre 2017

Perl random string, but doesn't seem so random when used in a browser

This is driving me nuts! I have a script that needs to create a random id, so that the file can be renamed. Here is a test I have:

for (my $i = 0; $i <= 100; $i++) {
    my $test = rand_id();
    print "FOO: $test\n";
}

sub rand_id {
# Used for the file names (not temp_id, for new adds)
  use String::Random qw(random_regex random_string);
  my $rand = random_regex("[a-zA-Z0-9]"x20);
  return $rand;
}

This works as 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 good (no repeats).

I then have almost the same code (just not in a loop) in my script, but when I keep reloading 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" ids. What am I doing wrong?

The module I'm using is http://ift.tt/2gvkEKG , but I'm getting the same problem when doing stuff with even something like:

my $rand = Digest::MD5::md5_base64( rand );

Aucun commentaire:

Enregistrer un commentaire