#27683 [Opn->Csd]: rand() and mt_rand() require seeding
| From: | derick@php.net | Date: | Thu, 25 Mar 2004 14:32:06 +0000 |
| Subject: | #27683 [Opn->Csd]: rand() and mt_rand() require seeding | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-56811@lists.php.net to get a copy of this message | ||
ID: 27683
Updated by: derick@php.net
Reported By: x13 at rpgsource dot net
-Status: Open
+Status: Closed
Bug Type: Math related
Operating System: Windows 2000 Professional (SP3)
PHP Version: 4.3.4
New Comment:
This bug has been fixed in CVS.
Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
Thank you for the report, and for helping us make PHP better.
Previous Comments:
------------------------------------------------------------------------
[2004-03-24 18:08:52] x13 at rpgsource dot net
Description:
------------
Hello,
I run a script using the command-line interface which generates three
random numbers and writes them, along with the date, to a logfile.
Using timers on the mIRC chat program, I run this script every 45
seconds:
dos SILENT C:\apache\php\php.exe D:\htdocs\private\sites\fake\fake.php
The "dos SILENT" command simply opens cmd.exe in the background and
executes whatever parameters I pass. It's equivalent to:
C:\>C:\apache\php\php.exe D:\htdocs\private\sites\fake\fake.php
I have many other applications (including other PHP scripts) running in
this manner. I am 99.9% sure the way I execute php.exe (with "dos
SILENT") has nothing to do with this problem.
I run Apache/1.3.29 (Win32) with a basically-standard httpd.conf file
and PHP 4.3.4 with an unedited php.ini file. At first, using rand(),
the random numbers were being generated just fine. I checked the
logfile a while later and noticed that identical random numbers were
being chosen and logged:
03/23/04, 23:10:21 (8, 1, 358)
03/23/04, 23:11:02 (8, 1, 358)
03/23/04, 23:11:55 (8, 1, 358)
03/23/04, 23:12:36 (8, 1, 358)
I tried switching to mt_rand(); the problem persisted. I then tried
seeding the random number generator once, using the example provided in
the manual, and it began working correctly:
03/24/04, 07:36:33 (8, 1, 634)
03/24/04, 07:37:18 (8, 0, 680)
03/24/04, 07:38:03 (4, 0, 677)
03/24/04, 07:38:48 (4, 0, 10)
I found nothing mentioned in the bug database or in the user comments
for rand()'s manual section. I'd be willing to experiment further, if
necessary; just contact me. Thank you.
Reproduce code:
---------------
<?php
function make_seed() {
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
mt_srand(make_seed());
$rand = array(mt_rand(1,9), mt_rand(0,1), mt_rand(1,700));
$log_p = fopen("log.txt", "a");
fwrite($log_p, date("m/d/y, H:i:s")." ($rand[0], $rand[1],
$rand[2])\n");
fclose($log_p);
?>
Expected result:
----------------
I expected three random numbers to be generated and written to log.txt,
such as:
03/24/04, 07:36:33 (8, 1, 634)
03/24/04, 07:37:18 (8, 0, 680)
03/24/04, 07:38:03 (4, 0, 677)
03/24/04, 07:38:48 (4, 0, 10)
Actual result:
--------------
Without the code:
function make_seed() {
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
mt_srand(make_seed());
...my results were:
03/23/04, 23:10:21 (8, 1, 358)
03/23/04, 23:11:02 (8, 1, 358)
03/23/04, 23:11:55 (8, 1, 358)
03/23/04, 23:12:36 (8, 1, 358)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=27683&edit=1