In article <[EMAIL PROTECTED]>, Sean M. Burke 
<[EMAIL PROTECTED]> wrote:

> 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);
>    }

here's the patch.  if no one complains after a few days i'll commit it.

Index: perlfaq.pod
===================================================================
RCS file: /cvs/public/perlfaq/perlfaq.pod,v
retrieving revision 1.7
diff -u -d -r1.7 perlfaq.pod
--- perlfaq.pod 31 Jan 2002 04:27:54 -0000      1.7
+++ perlfaq.pod 11 Mar 2002 01:22:30 -0000
@@ -341,6 +341,10 @@
 
 =item *
 
+How do I get a random number between X and Y?
+
+=item *
+
 How do I find the week-of-the-year/day-of-the-year?
 
 =item *
Index: perlfaq4.pod
===================================================================
RCS file: /cvs/public/perlfaq/perlfaq4.pod,v
retrieving revision 1.17
diff -u -d -r1.17 perlfaq4.pod
--- perlfaq4.pod        4 Mar 2002 21:30:43 -0000       1.17
+++ perlfaq4.pod        11 Mar 2002 01:22:32 -0000
@@ -346,6 +346,20 @@
 pseudorandom generator than comes with your operating system, look at
 ``Numerical Recipes in C'' at http://www.nr.com/ .
 
+=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);
+   }
+
 =head1 Data: Dates
 
 =head2 How do I find the week-of-the-year/day-of-the-year?

Reply via email to