Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

return truely random number?
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

Report this thread to moderator Post Follow-up to this message
Old Post
sam
12-23-04 09:07 PM


Re: return truely random number?
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?"

Report this thread to moderator Post Follow-up to this message
Old Post
Chris Mattern
12-23-04 09:07 PM


Re: return truely random number?
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.


Report this thread to moderator Post Follow-up to this message
Old Post
ajs@ajs.com
12-23-04 09:07 PM


Re: return truely random number?
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

Report this thread to moderator Post Follow-up to this message
Old Post
sam
12-24-04 02:03 AM


Re: return truely random number?
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";

Report this thread to moderator Post Follow-up to this message
Old Post
Joe Smith
12-26-04 02:01 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Miscellaneous archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 08:13 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.