Bug #12455 Updated: Srand and shuffle give odd results
| From: | sulka at sulake dot com | Date: | Tue, 31 Jul 2001 14:20:27 +0000 |
| Subject: | Bug #12455 Updated: Srand and shuffle give odd results | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-61645@lists.php.net to get a copy of this message | ||
ID: 12455
User updated by: sulka@sulake.com
Reported By: sulka@sulake.com
Status: Bogus
Bug Type: *Math Functions
Operating System: All
PHP Version: 4.0.4pl1, 4.0.6
New Comment:
Ok, here's sample code that should wake you up. The problem
occurs even when srand is only called once / page load.
Create two PHP pages:
password.php
<?PHP
$password = "";
$array =
array('a','b','c','d','e','f','g','h','i','j','k','l','m','
n','o','p','q','r','s','t','u','v','w','x','y','z',0,1,2,4,
5,6,7,8,9);
srand ((double)microtime()*1000000);
shuffle(&$array);
for ($i=0; $i<6; $i++) { $password .= $array[$i]; }
echo $password;?>
load100times.php
<?PHP
$i=0;
while($i<100) {
$file = fopen ("http://localhost/password.php", "r");
$password = fgets ($file, 128);
fclose($file);
$a[$password] = $password;
$i++;
}
echo "Count: " . count($a);
?>
Then load the load100times.php in your browser. The result
should be 100 but is still 4 on the machines I'm testing
on. I'm assuming srand is not ment to be run only once /
server start?
Previous Comments:
------------------------------------------------------------------------
[2001-07-31 10:00:28] jflemer@php.net
Try moving your srand() call outside of the pwd() function. That should fix it. The posted script
returns 4 for me on Solaris 5.8, and 500 when I move the srand() outside of pwd().
srand() should only be called *once*.
Reopen this if that does not fix it.
------------------------------------------------------------------------
[2001-07-30 06:25:27] sulka@sulake.com
The same problem also occurs on PHP 4.0.6 on Darwin/PPC.
System info:
PHP Version 4.0.6
System: Darwin localhost 1.3.7 Darwin Kernel Version 1.3.7:
Sat Jun 9 11:12:48 PDT 2001;
root:xnu/xnu-124.13.obj~1/RELEASE_PPC Power Macintosh
powerpc
Configure Command './configure' '--with-mysql=/usr/local'
'--with-apxs=/usr/sbin/apxs' '--with-zlib=/usr'
Server API: Apache
Apache Version: Apache/1.3.20
------------------------------------------------------------------------
[2001-07-30 05:17:09] sulka@sulake.com
A friend tried to run the test code on his machine and also
got 4 as a result. This is a Linux machine so my original
guess of this being Solaris specific isn't true. The only
real common denominator seems to be the PHP version? I'm
trying to get an upgrade to see if this is happening under
4.0.6.
Here's more info on the two servers this is occuring on:
PHP Version: 4.0.4pl1
System:
Linux xxx.com 2.2.14 #2 SMP Tue Mar 14 14:42:34 PST 2000
i686 unknown
Configure Command: './configure'
'--with-apxs=/usr/local/apache/bin/apxs' '--with-xml'
'--with-mcrypt' '--with-mysql' '--enable-track-vars'
Server API: Apache
System: SunOS yyy.com 5.8 Generic_108528-02 sun4u sparc
SUNW,UltraSPARC-IIi-cEngine
Configure Command: './configure'
'--with-apache=../apache_1.3.14'
'--with-mysql=/usr/local/mysql' '--with-ldap=/usr/local'
'--with-db3=/usr/local/BerkeleyDB.3.2' '--with-gd=../gd1.5'
'--with-ttf=/usr/local' '--enable-wddx'
------------------------------------------------------------------------
[2001-07-29 16:41:51] sulka@sulake.com
Well, when I run that code I get 4, not 500. Upping the
number of iterations doesn't help. I think the problem is
with the particular build (extension/OS combination). Any
way to debug this?
------------------------------------------------------------------------
[2001-07-29 14:26:39] rasmus@php.net
I don't think I understand what the problem is here. I tested your code with the following:
<?
function pwd() {
$password = "";
$array =
array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',0,1,2,4,5,6,7,8,9);
srand ((double)microtime()*1000000);
shuffle(&$array);
for ($i=0; $i<6; $i++) { $password .= $array[$i]; }
return $password;
}
$i=0;
while($i<500) {
$password = pwd();
$a[$password] = $password;
$i++;
}
echo count($a);
?>
This always returns 500 which means that 500 unique passwords were always created.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://bugs.php.net/?id=12455
Edit this bug report at http://bugs.php.net/?id=12455&edit=1