It occurs to me that the following was a frequently asked question on
EFNet #perl, when I hung around there. So here's my suggested addition for
to perlfaq4, which at the moment seems to have nothing like this:
=head2 How do I get a random number between X and Y?
Use the following simple function. It selects a random integer between
(and possibly including!) the two given integers, e.g.,
C<random_int_in(50,120)>
sub random_int_in ($$) {
my($min, $max) = @_;
# Assumes that the two arguments are integers themselves!
return $min if $min == $max;
($min, $max) = ($max, $min) if $min > $max;
return $min + int rand(1 + $max - $min);
}
--
Sean M. Burke [EMAIL PROTECTED] http://www.spinn.net/~sburke/