[svn:perlfaq] r8408 - perlfaq/trunk
[email protected] Mon, 18 Dec 2006 10:42:27 -0800 (PST)
| Newsgroups | perl.cvs.perlfaq |
|---|---|
| Message-ID | <[email protected]> |
Author: comdog Date: Mon Dec 18 10:42:26 2006 New Revision: 8408 Modified: perlfaq/trunk/perlfaq4.pod Log: * How do I get a random number between X and Y? + finished incomplete sentence Modified: perlfaq/trunk/perlfaq4.pod ============================================================================== --- perlfaq/trunk/perlfaq4.pod (original) +++ perlfaq/trunk/perlfaq4.pod Mon Dec 18 10:42:26 2006 @@ -362,17 +362,16 @@ =head2 How do I get a random number between X and Y? -To get a random number between two values, you can use the -C<rand()> builtin to get a random number between 0 and +To get a random number between two values, you can use the C<rand()> +builtin to get a random number between 0 and 1. From there, you shift +that into the range that you want. + +C<rand($x)> returns a number such that C<< 0 <= rand($x) < $x >>. Thus +what you want to have perl figure out is a random number in the range +from 0 to the difference between your I<X> and I<Y>. -C<rand($x)> returns a number such that -C<< 0 <= rand($x) < $x >>. Thus what you want to have perl -figure out is a random number in the range from 0 to the -difference between your I<X> and I<Y>. - -That is, to get a number between 10 and 15, inclusive, you -want a random number between 0 and 5 that you can then add -to 10. +That is, to get a number between 10 and 15, inclusive, you want a +random number between 0 and 5 that you can then add to 10. my $number = 10 + int rand( 15-10+1 );