X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlfaq4.pod;h=6a882c53ff5b2c6718b0d0549ddd9a51b60ed1dc;hb=938c8732ceb115a707f725327a631eb35319ba87;hp=7d3a5bcb5a299d5c54b35c5a26303e7f3b0d2c33;hpb=4dfcc30baac9af2364bced02447a6d9d74693f53;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlfaq4.pod b/pod/perlfaq4.pod index 7d3a5bc..6a882c5 100644 --- a/pod/perlfaq4.pod +++ b/pod/perlfaq4.pod @@ -1,6 +1,6 @@ =head1 NAME -perlfaq4 - Data Manipulation ($Revision: 1.49 $, $Date: 2003/09/20 06:37:43 $) +perlfaq4 - Data Manipulation ($Revision: 1.54 $, $Date: 2003/11/30 00:50:08 $) =head1 DESCRIPTION @@ -362,9 +362,20 @@ pseudorandom generator than comes with your operating system, look at =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 +C 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 and I. + +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 ); + +Hence you derive the following simple function to abstract +that. It selects a random integer between the two given +integers (inclusive), For example: C. sub random_int_in ($$) { my($min, $max) = @_; @@ -415,14 +426,6 @@ Use the following simple functions: return 1+int((((localtime(shift || time))[5] + 1899))/1000); } -You can also use the POSIX strftime() function which may be a bit -slower but is easier to read and maintain. - - use POSIX qw/strftime/; - - my $week_of_the_year = strftime "%W", localtime; - my $day_of_the_year = strftime "%j", localtime; - On some systems, the POSIX module's strftime() function has been extended in a non-standard way to use a C<%C> format, which they sometimes claim is the "century". It isn't, @@ -1489,16 +1492,11 @@ the hash is to be modified. Use the rand() function (see L): - # at the top of the program: - srand; # not needed for 5.004 and later - - # then later on $index = rand @array; $element = $array[$index]; -Make sure you I. -If you are calling it more than once (such as before each -call to rand), you're almost certainly doing something wrong. +Or, simply: + my $element = $array[ rand @array ]; =head2 How do I permute N elements of a list? @@ -1586,7 +1584,7 @@ If you need to sort on several fields, the following paradigm is useful. This can be conveniently combined with precalculation of keys as given above. -See the F artitcle article in the "Far More Than You Ever Wanted +See the F article in the "Far More Than You Ever Wanted To Know" collection in http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz for more about this approach.