You are viewing the version of this documentation from Perl 5.14.1. View the latest version
srand EXPR
srand

Sets and returns the random number seed for the rand operator.

The point of the function is to "seed" the rand function so that rand can produce a different sequence each time you run your program. When called with a parameter, srand uses that for the seed; otherwise it (semi-)randomly chooses a seed. In either case, starting with Perl 5.14, it returns the seed.

If srand() is not called explicitly, it is called implicitly without a parameter at the first use of the rand operator. However, this was not true of versions of Perl before 5.004, so if your script will run under older Perl versions, it should call srand; otherwise most programs won't call srand() at all.

But there are a few situations in recent Perls where programs are likely to want to call srand. One is for generating predictable results generally for testing or debugging. There, you use srand($seed), with the same $seed each time. Another other case is where you 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 /dev/urandom device if available. And still another case is that you may want to call srand() after a fork() to avoid child processes sharing the same seed value as the parent (and consequently each other).

Do not call srand() (i.e., without an argument) more than once per process. The internal state of the random number generator should contain more entropy than can be provided by any seed, so calling srand() again actually loses randomness.

Most implementations of srand take an integer and will silently truncate decimal numbers. This means srand(42) will usually produce the same results as srand(42.1). To be safe, always pass srand an integer.

In versions of Perl prior to 5.004 the default seed was just the current time. This isn't a particularly good seed, so many old programs supply their own seed value (often time ^ $$ or time ^ ($$ + ($$ << 15))), but that isn't necessary any more.

For cryptographic purposes, however, you need something much more random than the default seed. Checksumming the compressed output of one or more rapidly changing operating system status programs is the usual method. For example:

srand (time ^ $$ ^ unpack "%L*", `ps axww | gzip -f`);

If you're particularly concerned with this, search the CPAN for random number generator modules instead of rolling out your own.

Frequently called programs (like CGI scripts) that simply use

time ^ $$

for a seed can fall prey to the mathematical property that

a^b == (a+1)^(b+1)

one-third of the time. So don't do that.

A typical use of the returned seed is for a test program which has too many combinations to test comprehensively in the time available to it each run. It can test a random subset each time, and should there be a failure, log the seed used for that run so that it can later be used to reproduce the same results.