Solved

random number generation

Posted on 1998-07-26
8
164 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl script to count sepecial characters in a file 7 147
remove duplicates from the csv file 13 115
Perl Awk Need Help 3 129
Regular Expression for URL 10 102
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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

830 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