Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

true 6 random numbers...

Status
Not open for further replies.

perlone

Programmer
Joined
May 20, 2001
Messages
438
Location
US
I don't know that much about creating a true random numbers. I want to make 6 random numbers like 125758. Anybody knows how? I use the following but it gives me one of those nonsense numebers:

$random_numbers = srand(6);

That gives you like: 2.99826324451715. Which is not a good help. Anybody knows?

-Aaron.

 
Aaron,

Have another look at the documentation for srand, I don't think it's doing quite what you want it to:

[tt]perldoc -f srand[/tt]
Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
There is a module called Math::TruelyRandom I believe that you can download from CPAN and it does a pretty good job at creating a truely random number.

Hope this helps,

-Vic vic cherubini
vikter@epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
Since rand returns a random value between 0 and 1, you can just multiply the result by 1000000 and take the integer value of that:
Code:
$rnum = int(rand * 1000000);
Note that since this may give numbers with leading zeroes, you should probably check the size:
Code:
$rnum = 0;
while ( $rnum < 99999 ) {
   $rnum = int(rand() * 1000000);
}
I just tested the above and it works fine.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top