Re: Sinister variable caching problem with rand()

Anthony Esposito <[email protected]>
Newsgroups gmane.comp.apache.mod-perl
Message-ID <[email protected]>
Thank you everyone for helping me, I am surprised by the support our there
for mod_perl.

Cosimo sent me a quick fix to add to the startup.pl file, I have tested and
verified it. The code below works with no problems after adding:

 my $srv = Apache2::ServerUtil->server;
 $srv->push_handlers(
PerlChildInitHandler => sub { srand(time ^ $$) } );

This fix not only corrected the rand() example I posted, but fixed my root
issue that rand() represented in my test script. My actual script uses:

my $id = new String::Random;
my $num = $id->randregex('[A-Za-z0-9]{30}');

This code is in a sub in another library. For the sake of keeping it simple
I removed the use of it for my example. Luckily, String::Random must have
suffered from the same rand() problem : )

And I HIGHLY appreciate Michael Peters tip (UUID() function in mysql), I
will most likely start using that in place of my own random keys to reduce
my overhead and worries.

The reason I am not using auto_increment is because the databases exist on a
mysql cluster. The auto_increment counts would have to be maintained very
carefully with multiple servers running the same database so I opted to not
worry about it and generate completely unique keys. When a line is added in
one database it is advertised to all others, the linking between lines in
diff tables is easier this way too.

Thanks a ton gentlemen, you have saved me a great deal of time.

Anthony Esposito

On Sat, May 15, 2010 at 1:03 PM, Anthony Esposito <[email protected]
> wrote:

> In one of my programs I started to receive database errors for not having a
> unique id. I generate unique ids for each of the mysql lines that I add to
> the database. I realized that the perl variable $idNum was keeping the same
> random string for multiple executions.
>
> I created a test program to demonstrate the principle of what is happening.
>
> I have shown that the $count variable will function properly, I understand
> why and expect $idNum to work the same way. However, when $idNum =
> rand(99999999999) is used it misbehaves and starts to show the same numbers
> after enough refreshes.
>
> The code stores the random numbers in a file so I can detect them over
> multiple sessions. Within the same session a random key has never been
> generated twice.
>
> Please help, I have been working on this for 2 days and it is killing my
> progress at work.
>
> -------------------------------------------------------------------------
>
> use strict;
>
> my $idNum=0;
> my $count=0;
> for(1..10){
> my %hash;
> open(data,"<keys.txt");
> while(<data>){
> chomp;
> $hash{$_}=$_;
> }
> close(data);
>
> $idNum = rand(9999999999999);
> $count++;
> open(data2,">>keys.txt");
> print data2 "$idNum\n";
> close(data2);
>
> if ($hash{$idNum}){ print "DUPLICATE - $idNum<br>\n";}else{ print "UNIQUE
> $idNum<br>\n"; }
>
> }
>
> print "$count";
>
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.