php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42465 rand() and mt_rand() return same values from forked processes
Submitted: 2007-08-28 19:31 UTC Modified: 2007-08-29 08:59 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: bugs at nazarenko dot net Assigned:
Status: Not a bug Package: Math related
PHP Version: 5CVS-2007-08-28 (snap) OS: Linux, Solaris
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: bugs at nazarenko dot net
New email:
PHP Version: OS:

 

 [2007-08-28 19:31 UTC] 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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-08-28 22:09 UTC] bugs at nazarenko dot net
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...
 [2007-08-28 22:22 UTC] bugs at nazarenko dot net
... and if I understand right I am being suggested to use srand() or mt_srand(), which according to the documentation is not required since PHP 4.2.

So what I am missing?
 [2007-08-29 08:59 UTC] jani@php.net
It doesn't count for run-once SAPIs like CLI.

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jul 04 15:01:36 2025 UTC