[San-diego-pm] another bug in perl: rand/OSX?

Randal L. Schwartz merlyn at stonehenge.com
Thu Jun 3 09:25:44 CDT 2004

>>>>> "C" == C Abney <cabney at ucsd.edu> writes:

C> Maybe my keywords suck and this is a known issue?
C> Running this script on a Mac OSX gives very unexpected results:

You can illustrate this well known bug with:

perl -le 'print rand; fork or exit print rand; fork or exit print rand; fork or exit print rand; print rand'

You'll get one number from the first rand call (which sets the seed),
and then the same number from all remaining calls to rand.

The solution is well known... when you fork, you need to srand manually
in the child.

The behavior has nothing to do with OSX.  It's completely generic to Perl.
I just verified that on my OpenBSD box.

Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn at stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

More information about the San-Diego-pm mailing list