#25007 [NEW]: rand & mt_rand seed RNG every call
| From: | dcowgill at mail dot communityconnect dot com | Date: | Sun, 10 Aug 2003 23:20:08 +0000 |
| Subject: | #25007 [NEW]: rand & mt_rand seed RNG every call | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-45670@lists.php.net to get a copy of this message | ||
From: dcowgill at mail dot communityconnect dot com
Operating system: Linux
PHP version: 4.3.3RC3
PHP Bug Type: Performance problem
Bug description: rand & mt_rand seed RNG every call
Description:
------------
From the docs for rand: "In older versions of PHP, you
had to seed the random number generator before use with
srand(). Since 4.2.0 this is no longer necessary."
The implication is that rand seeds the RNG the first
time it is called, unless srand has been previously
called; however, rand seeds the RNG every time (or
until srand is explicitly called). This results in a
significant increase in running time (about an order of
magnitude).
The above also applies to mt_rand, except that the
performance penalty is greater (apparently because its
seed-generation algorithm is more expensive).
(the patch to ext/standard/rand.c to correct the
problem is trivial)
Reproduce code:
---------------
// t1.php
<?
for ($i=0; $i<100000; $i++) rand(0,10);
?>
// t2.php
<?
srand();
for ($i=0; $i<100000; $i++) rand(0,10);
?>
Actual result:
--------------
$ time php t1.php
real 0m0.729s
user 0m0.650s
sys 0m0.080s
$ time php t2.php
real 0m0.101s
user 0m0.100s
sys 0m0.000s
--
Edit bug report at http://bugs.php.net/?id=25007&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=25007&r=trysnapshot4
Try a CVS snapshot (php5): http://bugs.php.net/fix.php?id=25007&r=trysnapshot5
Fixed in CVS: http://bugs.php.net/fix.php?id=25007&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=25007&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=25007&r=needtrace
Try newer version: http://bugs.php.net/fix.php?id=25007&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=25007&r=support
Expected behavior: http://bugs.php.net/fix.php?id=25007&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=25007&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=25007&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=25007&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=25007&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=25007&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=25007&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=25007&r=gnused