For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > December 2004 > return truely random number?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author return truely random number?
sam

2004-12-23, 4:07 pm

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
Chris Mattern

2004-12-23, 4:07 pm

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):


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?"
ajs@ajs.com

2004-12-23, 4:07 pm

Other posts point out the flaw in your code, but you might also want to
look at Math::TrulyRandom, which I like and use often.

sam

2004-12-23, 9:03 pm

Just 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

Joe Smith

2004-12-25, 9:01 pm

sam 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";
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com