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
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
ASKER
every time you execute it, it will produce the same output.
What if you type
srand;
at the beginning of your script?
srand;
at the beginning of your script?
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)
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)
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
Way cool, man
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The following works for me:
$x = 1 + int(rand(6));
print "$x\n"