note 117967 deleted from function.srand by cmb

From: Date: Wed, 09 Sep 2015 23:16:37 +0000
Subject: note 117967 deleted from function.srand by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-203761@lists.php.net to get a copy of this message
Note Submitter: Glauco Lins ---- srand and mt_srand are both initialized only once per process ID. You cannot re-seed your rand algorithms after the first "srand", "mt_srand", "rand", "mt_rand", "shuffle", or any other rand-like function. I have been facing an issue where after forking my process, all childs were generating exactly the same rand values. This was due a first "shuffle" call on the parent process, so I could not re-seed the childs. To solve my issue, I simple called "rand" N times, to offset the child rand generators. # Offset the child rand generator by its PID $n = (getmypid() % 100) * (10 * abs(microtime(true) - time())); for ($n; $n > 0; $n--) { rand(0, $n); } Since each pcntl_fork takes a while to be completed, the microtime offers an extra offset, other than one PID increment. This small code will make at the WORST hypothesis 1000 iterations.

« previous php.notes (#203761) next »