[San-diego-pm] [Fwd: Re: [perl #30028] rand broken on Mac OSX (G5), was Re: problem with rand on OSX]

C. Abney cabney at ucsd.edu
Thu Jun 3 10:53:21 CDT 2004


-----Forwarded Message-----

From: Dominic Dunlop <domo at computer.org>
To: perl5-porters at perl.org
Cc: C. Abney (via RT) <perlbug-followup at perl.org>
Subject: Re: [perl #30028] rand broken on Mac OSX (G5), was Re: problem with rand on OSX
Date: 03 Jun 2004 16:59:11 +0200

Summary: fixed in 5.8.4.

On 3 Jun 2004, at 04:21, C. Abney (via RT) wrote:
> #! /usr/bin/perl -w
> use strict;
> my @jobs;
> for (0..2)
> {
>         defined( my $pid = fork() ) or die "my BABY!!!";;
>         last if $pid == 0;
>         push @jobs, $pid;
> }
> if ( 3 == @jobs ) {
>         $SIG{CHLD} = 'IGNORE';
>         sleep 1;
> } else {
>         my $slp = int rand 20;
>         print "child gets $slp second(s)\n";
>         exit 0;
> }
Well, on my Mac OS 10.3.4 (with a G3 processor) that indeed gives

child gets 6 second(s)
child gets 6 second(s)
child gets 6 second(s)

(or similar) with perl5.8.1rc3 (as shipped with Mac OS) or with a 
freshly-built perl5.8.1. Given that the random number seed is stored in 
memory that gets cloned from the parent to each child, and each child 
applies the same algorithm to the seed to get the next random number, 
this is the behaviour I'd expect. However, I get

child gets 19 second(s)
child gets 11 second(s)
child gets 0 second(s)

(or similar) with perl5.8.4, the current stable release, and with 5.9.2 
on the development track.

Both perls are using the same random number generator:
$ perl '-V:rand.*'  # either perl version

What seems to have changed is where the random number generator seed 
gets set: in 5.8.1, it appears to be in the parent before any call to 
rand(), so each child gets the same seed; in 5.8.4 the setting seems to 
be delayed until the first use of rand() -- so each child gets a 
different seed. Adding a line


near the top of the script to force the setting of the seed in the 
parent brings back the 5.8.1 behaviour.
I can't see a patch between 5.8.1 and 5.8.4 which claims to have 
anything to do with random numbers so I don't know what changed the 
behaviour. Can anybody shed any light?

However, your solution is to upgrade to 5.8.4.

> Perlbug is not set up on that machine

It's delivered with Mac OS X. You can usually get it to do something 
somewhat useful -- like save a report to a file for later mailing by 
some other means -- with command line options. Failing that, please 
include the output of  perl -V  in reports. Thanks.
Dominic Dunlop

Charles Abney
Polymorphism Research Laboratory, 0603
UCSD School of Medicine
9500 Gilman Dr.
La Jolla, CA 92093-0603
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 307 bytes
Desc: This is a digitally signed message part
Url : http://mail.pm.org/pipermail/san-diego-pm/attachments/20040603/2456edd4/attachment.bin

More information about the San-Diego-pm mailing list