#42465 [Bgs->Opn]: rand() and mt_rand() return same values from forked processes
ID: 42465
User updated by: bugs at nazarenko dot net
Reported By: bugs at nazarenko dot net
-Status: Bogus
+Status: Open
Bug Type: Math related
Operating System: Linux, Solaris
PHP Version: 5CVS-2007-08-28 (snap)
New Comment:
I am sorry but I have been through these pages before submitting the
report.
I know you are very busy people, but may I kindly ask you to provide
tiny bit more information as I could not find anything related to
pcntl_fork() problem that I experience on those manual pages.
Thank you...
Previous Comments:
------------------------------------------------------------------------
[2007-08-28 21:37:34] jani@php.net
http://php.net/mt_srand
http://php.net/srand
------------------------------------------------------------------------
[2007-08-28 19:31:32] bugs at nazarenko dot net
Description:
------------
Using CLI, if more than 1 process is forked with pcntl_fork() both
functions mt_rand() and rand() return same values in all child
processes.
This only happens when there was a call to mt_rand()/rand() in the
parent process before forking.
Reproduce code:
---------------
Compiled latest available snapshot under Solaris 10 and SuSE Linux 10.1
with the following configure options:
--disable-all --disable-cgi --enable-pcntl --disable-ipv6
The test script is the following:
<?php
echo mt_rand(0,255)."\n"; # comment this line out to see difference
for ($i=0; $i<3; $i++)
{
if (pcntl_fork() == 0)
{
die(mt_rand(0,255)."\n");
}
}
?>
Expected result:
----------------
Expected result is 4 random numbers generated by 4 different Unix
processes (1 parent and 3 children).
Actual result:
--------------
The actual result (both on Solaris and Linux) is that all 3 numbers
generated by the children are the same.
Now comment out the first echo command belonging to the parent process
and the result is 3 random numbers generated by children.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=42465&edit=1
Thread (5 messages)