Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm quite new to perl programming. Here's the problem: I can't get my loop to generate more than 3 random numbers, and I can't figure out why. The command line arguments provide the amount of numbers I want, the minimum and maximum of those values. Here's the important part of code I used:

$n=$ARGV[0]; $a=$ARGV[1]; $b=$ARGV[2]; my $range = $b-$a; for ($i=0,$i<=$n,$i++){ $random_number= int((rand($range)) + $a); print $random_number."\n"; push (@numbers, $random_number); }

appreciate the help!

Replies are listed 'Best First'.
Re: for loop
by moritz (Cardinal) on Nov 04, 2011 at 09:29 UTC

    That's because the syntax of the for loop is wrong; you probably want ; instead of , in there.

    With the commas it's just a 3-element list (0, 1, 0) over which the for iterates. But you can write that even easier:

    for (1..$n) { # body of the loop goes here }

    See perlsyn for more information about the syntax of for-loops (scroll down to subsection "Foreach Loops").

      thanks a lot! just couldn't see it by myself :)