Solved

random number generation

Posted on 1998-07-26
8
161 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
  • 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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 …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now