in reply to Re: Creating a random generator
in thread Creating a random generator
Given a number, as in rand(6), it generates an integer which is at least 0 and less than the number it was given (0 to 5 in this case)
Almost, but not quite.
% perl -le 'print rand(6)' 4.45325434943245
This will take the number between 0 and less than 1 and multiply it by 6, nearly always leaving stuff behind the decimal point.
If you really don't want the fractional part you have to shave it off with either int($num) or sprintf('%.0f', $num). In the present case it'll work just fine, since Perl will Do The Right Thing and treat the number as an integer anyway, in order to use it as an index into the array. But were the OP to print the number itself, they may be surprised by the result.
• another intruder with the mooring in the heart of the Perl
|
|---|