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
tianAsked:
Who is Participating?
 
ozoConnect With a Mentor Commented:
5.004 seems a more perfect solution.
martinag's solution may be adequate if you don't run it too often.
srand($$+time); should suffice otherwise.
http://lavarand.sgi.com is indeed cool, but even slower than Math::TrulyRandom :-)
0
 
mlevCommented:
What was wrong with rand()?
The following works for me:

$x = 1 + int(rand(6));
print  "$x\n"
0
 
tianAuthor Commented:
every time you execute it, it will produce the same output.
0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
martinagCommented:
What if you type
srand;
at the beginning of your script?
0
 
ozoCommented:
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)
0
 
tianAuthor Commented:
Thanks all of you. But I think ozo's "srand($$+time); " is perfect solution.You want to submit it?  
0
 
b2piCommented:
you could also get a module (ReallyRandom, I think) from cpan, and use that for the seed)

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

Way cool, man
0
All Courses

From novice to tech pro — start learning today.