note 117966 added to function.srand
| From: | GlaucoLins | Date: | Wed, 09 Sep 2015 23:09:22 +0000 |
| Subject: | note 117966 added to function.srand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-203759@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 72.52.91.14
Probable Submitter: 189.14.1.241
----
Manual Page -- http://php.net/manual/en/function.srand.php
Edit -- https://master.php.net/note/edit/117966
Del: integrated -- https://master.php.net/note/delete/117966/integrated
Del: useless -- https://master.php.net/note/delete/117966/useless
Del: bad code -- https://master.php.net/note/delete/117966/bad+code
Del: spam -- https://master.php.net/note/delete/117966/spam
Del: non-english -- https://master.php.net/note/delete/117966/non-english
Del: in docs -- https://master.php.net/note/delete/117966/in+docs
Del: other reasons-- https://master.php.net/note/delete/117966
Reject -- https://master.php.net/note/reject/117966
Search -- https://master.php.net/manage/user-notes.php