16

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.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Ilan Kleiman
  • 1,149
  • 5
  • 18
  • 36
  • Integer or floating point? – TypeIA Feb 16 '14 at 02:09
  • @dvnrrs Integer, Sorry, I'll edit the question to include that – Ilan Kleiman Feb 16 '14 at 02:10
  • perl has good documentation; check it *before* looking for other random sites: http://perldoc.perl.org/perlfaq4.html#How-do-I-get-a-random-number-between-X-and-Y%3F (answer shown by toolic's suggested perldoc command) – ysth Feb 16 '14 at 05:46

2 Answers2

40
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.

TypeIA
  • 16,916
  • 1
  • 38
  • 52
5
my $random_num = int($minimum + rand($maximum - $minimum));
smonff
  • 3,399
  • 3
  • 36
  • 46
McLovin
  • 3,554
  • 1
  • 14
  • 15