php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27683 rand() and mt_rand() require seeding
Submitted: 2004-03-24 18:08 UTC Modified: 2004-03-25 09:32 UTC
From: x13 at rpgsource dot net Assigned:
Status: Closed Package: Math related
PHP Version: 4.3.4 OS: Windows 2000 Professional (SP3)
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: x13 at rpgsource dot net
New email:
PHP Version: OS:

 

 [2004-03-24 18:08 UTC] 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)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-03-25 09:32 UTC] derick@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 11:01:30 2024 UTC