From: Jarkko Hietaniemi Date: Tue, 4 Sep 2001 13:05:23 +0000 (+0000) Subject: More srand entry tweakage based on Randal's suggestion. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e0b236fe86fb5fced9182a324010842ee938ca4c;p=p5sagit%2Fp5-mst-13.2.git More srand entry tweakage based on Randal's suggestion. p4raw-id: //depot/perl@11862 --- diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod index ed144d1..50a02fe 100644 --- a/pod/perlfunc.pod +++ b/pod/perlfunc.pod @@ -4797,49 +4797,35 @@ loaded the standard Math::Complex module. Sets the random number seed for the C operator. -It's usually not necessary to call C at all, because if it is -not called explicitly, it is called implicitly at the first use of the -C operator. However, this was not the case in versions of Perl -before 5.004, so if your script will run under older Perl versions, it -should call C. - The point of the function is to "seed" the C function so that C can produce a different sequence each time you run your -program. Just do it B at the top of your program, or you -I get random numbers out of C. +program. -If EXPR is omitted, uses a semi-random value supplied by the kernel -(if it supports the F device) or based on the current -time and process ID, among other things. +If srand() is not called explicitly, it is called implicitly at the +first use of the C operator. However, this was not the case in +versions of Perl before 5.004, so if your script will run under older +Perl versions, it should call C. -Most implementations of C take an integer and will silently -truncate decimal numbers. This means C will usually -produce the same results as C. To be safe, always pass -C an integer. +Most programs won't even call srand() at all, except those that +need a cryptographically-strong starting point rather than the +generally acceptable default, which is based on time of day, +process ID, and memory allocation, or the F device, +if available. -Calling C multiple times is highly suspect. - -=over 4 - -=item * +You can call srand($seed) with the same $seed to reproduce the +I sequence from rand(), but this is usually reserved for +generating predictable results for testing or debugging. +Otherwise, don't call srand() more than once in your program. Do B call srand() (i.e. without an argument) more than once in a script. The internal state of the random number generator should contain more entropy than can be provided by any seed, so calling -srand() again actually I randomness. And you shouldn't use -srand() at all unless you need backward compatibility with Perls older -than 5.004. +srand() again actually I randomness. -=item * - -Do B call srand($seed) (i.e. with an argument) multiple times in -a script for any other purpose than calling it with the I -argument to produce the I sequence out of rand() I you -know exactly what you're doing and why you're doing it. Usually doing -anything else than reusing the same seed requires intimate knowledge of -the implementation of srand() and rand() on your platform. - -=back +Most implementations of C take an integer and will silently +truncate decimal numbers. This means C will usually +produce the same results as C. To be safe, always pass +C an integer. In versions of Perl prior to 5.004 the default seed was just the current C