Solved

random number generation

Posted on 1998-07-26
8
166 Views
Last Modified: 2010-03-05
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
0
Comment
Question by:tian
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 2

Expert Comment

by:mlev
ID: 1208788
What was wrong with rand()?
The following works for me:

$x = 1 + int(rand(6));
print  "$x\n"
0
 

Author Comment

by:tian
ID: 1208789
every time you execute it, it will produce the same output.
0
 
LVL 4

Expert Comment

by:martinag
ID: 1208790
What if you type
srand;
at the beginning of your script?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 84

Expert Comment

by:ozo
ID: 1208791
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
 

Author Comment

by:tian
ID: 1208792
Thanks all of you. But I think ozo's "srand($$+time); " is perfect solution.You want to submit it?  
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1208793
you could also get a module (ReallyRandom, I think) from cpan, and use that for the seed)

0
 
LVL 5

Expert Comment

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

Way cool, man
0
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 1208795
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question