DelphiFAQ Home Search:

Generating a random number

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

Question:

I need to generate a random number between 1 and 100. How do I do this with perl?

Answer:

You need two functions: srand() to initialize the random generator. This needs to be called only once at the beginning of your program. You may pass an argument to initialize srand, if you don't it will use a semi-random value based on the current time and process ID, among other things. If you think that you need really random random numbers e.g. for cryptography, then you should see the Math::TrulyRandom module in CPAN.

After initializing, use rand() each time you need a random number. rand() by default will return a value >= 0 and < 1.

Use rand(100) to get a value >=0 and < 100. Your code would look like this:

#!/usr/bin/perl

srand(); # initialize

my $num = rand(100) + 1;
# 1 <= $num <= 100

# picking a random array element
# this uses the $# operator to get the number 
# of elements in the array
my @options = ( 'red','yellow','green');
print $options[int rand scalar @options];
print $options[int rand $#options];

Comments:

2012-06-05, 21:34:38
anonymous from China  
<strong> http://www.coachout..strong> <a href=' http://www.coachout..g>Coach Outlet Online</strong></a><br />
<strong> http://www.coachfac..strong> <a href=' http://www.coachfac..g>Coach Factory</strong></a><br />
<strong> http://www.myloveco..strong> <a href=' http://www.myloveco..g>Coach Outlet Online</strong></a><br />
<strong> http://www.buyincoa..strong> <a href=' http://www.buyincoa..g>Coach Outlet</strong></a><br />
<strong> http://www.cheapcoa..strong> <a href=' http://www.cheapcoa..g>Cheap Coach Handbags</strong></a><br />
<strong> http://www.coachfac..strong> <a href=' http://www.coachfac..g>Coach Factory Outlet</strong></a><br />
<strong> http://www.buycoach..strong> <a href=' http://www.buycoach..g>Coach Outlet Online</strong></a><br />
<strong>www.coachoutletonfactory.net</strong> <a href=' http://www.coachout..g>Coach Factory Outlet</strong></a><br />
<strong>www.coachoutletonline4you.us</strong> <a href=' http://www.coachout..g>Coach Outlet Online</strong></a><br />
<strong>coachfactoryoutletq.net</strong> <a href=' http://www.coachfac..g>Coach Factory</strong></a><br />
<strong>gotocoachoutlets.net</strong> <a href=' http://www.gotocoac..g>Coach Outlet Online</strong></a><br />
2012-11-12, 02:13:47
coach001 from United Kingdom  

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: