Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: fork(), IO::Socket::SSL and rand()

by ikegami (Patriarch)
on Nov 19, 2008 at 16:23 UTC ( [id://724634]=note: print w/replies, xml ) Need Help??


in reply to Re: fork(), IO::Socket::SSL and rand()
in thread fork(), IO::Socket::SSL and rand()

It appears that you misunderstood. My fellow monks didn't say that calling srand is the solution. They said that calling srand is the problem.

Now, you could call srand in the children. But that's not a very good solution, since your seed is probably not going to be as good as Perl's. Ideally, what you want to do is delay srand getting called until after you've forked by avoiding calls to rand until after you've forked.

Since IO::Socket::SSL apparently calls rand on load, you need to delay its loading.

use strict; foreach (1 .. 5) { my $pid = fork(); next if $pid; require IO::Socket::SSL; <--- random(); random(); exit; } sub random { print sprintf("Process %d: %d", $$, int(rand(100000))), "\n"; }

Replies are listed 'Best First'.
Re^3: fork(), IO::Socket::SSL and rand()
by sgt (Deacon) on Nov 19, 2008 at 20:54 UTC

    Just feeling that somehow ikegami's answer could be better phrased:

    There are three points:

  • (added by me) calling srand() implicitly or explicitly with no seed should seed the way perl seeds (at least as the doc says for a perl version superior to 5.004)
  • then 'perldoc -f srand' says explicitly that one should not call srand() twice as you would lose randomness, so it seems that reseeding in each child is not the better option (one implicit call from use IO::Socket::SSL and one explicit call from each child)
  • finally actually delaying the implicit and only call to srand() can be done requiring IO::Socket::SSL in each child </it>

    IMHO this was the point made by fletch and at least another monk.

    cheers --stephan update: sorry not second post just answer

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://724634]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (1)
As of 2024-04-25 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found