Link to home
Start Free TrialLog in
Avatar of tian
tian

asked on

random number generation

hello,
How can I write a perl program to generate a random number between 1 and 6 and print it out. The condition is every time the program is executed,the random number should be different.(occasionally same is acceptable).  i have tried function rand() ,but it doesnot do the work.
Thanks
Avatar of mlev
mlev

What was wrong with rand()?
The following works for me:

$x = 1 + int(rand(6));
print  "$x\n"
Avatar of tian

ASKER

every time you execute it, it will produce the same output.
What if you type
srand;
at the beginning of your script?
Avatar of ozo
If you upgrade to 5.004, the srand; at the beginning would be done for you automatically.
Before 5.004, srand; would still give you the same number if you execute the program twice in the same second.
srand($$+time); should alleviate that problem.
(although a truly random number would still give you the same number about one out of six times)
Avatar of tian

ASKER

Thanks all of you. But I think ozo's "srand($$+time); " is perfect solution.You want to submit it?  
you could also get a module (ReallyRandom, I think) from cpan, and use that for the seed)

Oh, I forgot.  If you need truly, heavy-duty random, you can always use http://lavarand.sgi.com

Way cool, man
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial