Code Comments
Programming Forum and web based access to our favorite programming groups.Hi, I tried Math::Random and Math::Random::MT, but none of them can generate turely random number when I executed the following code in a short time frame (twice in a second): #!/usr/bin/perl #use Math::Random; use Math::Random::MT; $gen = Math::Random::MT->new($seed); # OR... $gen = Math::Random::MT->new(@seed); print $gen->rand(3)."\n"; #$random = random_uniform(); #$gmt = gmtime(); #print "Random: " . $random . "-" . $gmt . "\n"; I m trying to generate a transaction code for a data base table using the format of random-GMT Another question is how can I generate a gmt time in the format 92783456 instead of "Thu Dec 23 15:02:08 2004"? Thanks Sam
Post Follow-up to this messagesam wrote: > Hi, > > I tried Math::Random and Math::Random::MT, but none of them can generate > turely random number when I executed the following code in a short time > frame (twice in a second): I might note that *no* random number generator generates "truly random" numbers, because you can't get true random numbers from a deterministic procedure. However, I assume your actual complaint is that you're not getting back a suitable variety of pseudo-random numbers. > > #!/usr/bin/perl use warnings; use strict; > > #use Math::Random; > use Math::Random::MT; > > $gen = Math::Random::MT->new($seed); # OR... $gen = > Math::Random::MT->new(@seed); You never define a seed. So you always create a random generator with a seed of "0", so you always get the same sequence of psuedo-random numbers back. Warnings and strict would've told you of this problem. > > print $gen->rand(3)."\n"; > > #$random = random_uniform(); > #$gmt = gmtime(); > > #print "Random: " . $random . "-" . $gmt . "\n"; > > I m trying to generate a transaction code for a data base table using > the format of random-GMT > > Another question is how can I generate a gmt time in the format 92783456 > instead of "Thu Dec 23 15:02:08 2004"? perldoc -f time -- Christopher Mattern "Which one you figure tracked us?" "The ugly one, sir." "...Could you be more specific?"
Post Follow-up to this messageOther posts point out the flaw in your code, but you might also want to look at Math::TrulyRandom, which I like and use often.
Post Follow-up to this messageJust found one, Crypt::Random depend on /dev/random device, it really does give random number no matter how fast I generate the number... Sam. sam wrote: > Hi, > > I tried Math::Random and Math::Random::MT, but none of them can generate > turely random number when I executed the following code in a short time > frame (twice in a second): > > #!/usr/bin/perl > > #use Math::Random; > use Math::Random::MT; > > $gen = Math::Random::MT->new($seed); # OR... $gen = > Math::Random::MT->new(@seed); > > print $gen->rand(3)."\n"; > > #$random = random_uniform(); > #$gmt = gmtime(); > > #print "Random: " . $random . "-" . $gmt . "\n"; > > I m trying to generate a transaction code for a data base table using > the format of random-GMT > > Another question is how can I generate a gmt time in the format 92783456 > instead of "Thu Dec 23 15:02:08 2004"? > > Thanks > Sam
Post Follow-up to this messagesam wrote: > Another question is how can I generate a gmt time in the format 92783456 > instead of "Thu Dec 23 15:02:08 2004"? Familiarize yourself with the arguments to gmtime and its return value. $seconds_since_the_epoch = time; @time_array = gmtime($seconds_since_the_epoch); $time_scalar = gmtime($seconds_since_the_epoch); print "time=$seconds_since_the_epoch time_array=(@time_array) time_scalar='$time_scalar'\n";
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.