Ime Smits wrote:
> It seems that within Apache::ASP (probably mod_perl) the pseudo random
> number generator (rand) is not reinitialized (srand) when Apache forks a new
> process, so each child generates the same sequence of numbers using rand. In
> Apache::ASP 2.03, I can see [...]
> commented out. It's not the first time I hear that playing around with srand
> is bad, even perlfunc mentions that. Can anybody explain to me the reason?
> Shouldn't this srand thing actually be done somewhere in mod_perl?

and G.W.Haywood replied:
> It's staggeringly difficult to generate a truly random number using a
> computer.  People go to conferences about it.  Same problem with White
> Noise, which was more in my field when I was in that field.  It always
> turns out some shade of pink.  No kidding. [...]
> 
> On the other hand if you don't want all your Apache children to start
> with the same seed, I think you're probably justified in changing the
> seed.  That's what it's for.

Yes, perl doesn't reset the 'random generator initialized' status on
fork, which it arguably should:

$ perl -le 'srand; if (fork) { print rand } else { print rand }'
0.804962437599897
0.804962437599897

The workaround is to call srand explicitly in a ChildInitHandler.  Something
as simple as
PerlChildInitHandler "sub { srand }"
should work.

OTOH, the warnings are real: calling srand when not needed, or trying to
"help" it with explicit initialization values (time.$$ and similar
hacks) only worsens the quality of your random numbers.  Since perl
implicitly calls srand() at the first rand() call, there is no reason to
call srand() outside of forks, or when you actually want the sequences
to repeat.  And srand() doesn't need arguments; it's smart enough to
look for a decent source of randomness on its own (using /dev/urandom or
/dev/random or similar if it exists).

-- 
Roger Espel Llima, [EMAIL PROTECTED]
http://www.iagora.com/~espel/index.html

Reply via email to