cvsuser 02/03/11 13:32:24
Modified: . perlfaq.pod perlfaq4.pod
Log:
* added new FAQ: How do I get a random number between X and Y?
Revision Changes Path
1.8 +5 -1 perlfaq/perlfaq.pod
Index: perlfaq.pod
===================================================================
RCS file: /cvs/public/perlfaq/perlfaq.pod,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -w -r1.7 -r1.8
--- perlfaq.pod 31 Jan 2002 04:27:54 -0000 1.7
+++ perlfaq.pod 11 Mar 2002 21:32:23 -0000 1.8
@@ -1,6 +1,6 @@
=head1 NAME
-perlfaq - frequently asked questions about Perl ($Date: 2002/01/31 04:27:54 $)
+perlfaq - frequently asked questions about Perl ($Date: 2002/03/11 21:32:23 $)
=head1 DESCRIPTION
@@ -338,6 +338,10 @@
=item *
Why aren't my random numbers random?
+
+=item *
+
+How do I get a random number between X and Y?
=item *
1.18 +15 -1 perlfaq/perlfaq4.pod
Index: perlfaq4.pod
===================================================================
RCS file: /cvs/public/perlfaq/perlfaq4.pod,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -w -r1.17 -r1.18
--- perlfaq4.pod 4 Mar 2002 21:30:43 -0000 1.17
+++ perlfaq4.pod 11 Mar 2002 21:32:23 -0000 1.18
@@ -1,6 +1,6 @@
=head1 NAME
-perlfaq4 - Data Manipulation ($Revision: 1.17 $, $Date: 2002/03/04 21:30:43 $)
+perlfaq4 - Data Manipulation ($Revision: 1.18 $, $Date: 2002/03/11 21:32:23 $)
=head1 DESCRIPTION
@@ -345,6 +345,20 @@
random numbers, but this takes quite a while. If you want a better
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.