Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a random number between two numbers in Perl

Tags:

perl

I've seen many other ways to do this in other programming languages... Though I havent found one in Perl yet...

What I want to accomplish is to set two numbers:

$minimum = 100;
$maximum = 4000;

Then to create a random integer between those two. (whole number) ($random_num)

I've looked into this site: http://perlmeme.org/howtos/perlfunc/rand_function.html

Which is a good resource, although does not do exactly that.

like image 886
Ilan Kleiman Avatar asked Feb 16 '14 02:02

Ilan Kleiman


People also ask

How do you generate random numbers in terminal?

The random number or a range of random numbers can be generated using the $RANDOM variable. It generates a random number between 0 and 32767 by default. But you can set the range of numbers for generating random numbers by dividing the value of $RANDOM with a specific value.

Can you use math random in CSS?

Let me get that out of the way first — CSS has no built-in “random” function, no Math. random() equivalent and no way to generate a random number or a random color at all.


1 Answers

my $x = $minimum + int(rand($maximum - $minimum));

http://perldoc.perl.org/functions/rand.html

Note this range excludes $maximum itself. Add 1 to make it inclusive.

like image 127
TypeIA Avatar answered Sep 22 '22 21:09

TypeIA