From: x13 at rpgsource dot net
Operating system: Windows 2000 Professional (SP3)
PHP version: 4.3.4
PHP Bug Type: Math related
Bug description: rand() and mt_rand() require seeding
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 bug report at http://bugs.php.net/?id=27683&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=27683&r=trysnapshot4
Try a CVS snapshot (php5): http://bugs.php.net/fix.php?id=27683&r=trysnapshot5
Fixed in CVS: http://bugs.php.net/fix.php?id=27683&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=27683&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=27683&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=27683&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=27683&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=27683&r=support
Expected behavior: http://bugs.php.net/fix.php?id=27683&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=27683&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=27683&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=27683&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=27683&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=27683&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=27683&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=27683&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=27683&r=float