2.6.3. Discussion
This code generates and prints a random integer between 25 and 75,
inclusive:
$random = int( rand(51)) + 25;
print "$random\n";
The rand function returns a fractional number,
from (and including) 0 up to (but not including) its argument. We
give it an argument of 51 to get a number that can be 0 or more, but
never 51 or more. We take the integer portion of this to get a number
from 0 to 50, inclusive (50.99999.... will be turned into 50 by
int). We then add 25 to it to get a number from 25
to 75, inclusive.
$elt = $array[ rand @array ];
That's just like saying:
$elt = $array[ int( rand(0+@array) ) ];
Because rand is prototyped to take just one
argument, it implicitly imposes scalar context on that argument,
which, on a named array, is the number of elements in that array. The
function then returns a floating-point number smaller than its
argument and greater than or equal to zero. A floating-point number
used as an array subscript implicitly undergoes integer truncation
(rounding toward zero), producing in the end an evenly distributed,
randomly selected array element to assign to $elt.