|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2001-07-29 05:51 UTC] sulka at sulake dot com
I'm using the following code to create random strings
(passwords):
$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]; }
Now, for some reason this seems to return only five
different values ever on the Solaris machine I'm running
the code on. And I'm not checking on the same run of the
script, this is based on accessing page with the script
through http and looking at the results for about 150
reloads on two different days.
This is the first time I'm sending a PHP report so I don't
know exactly what information to provide. Please ask for
more if as you see fit.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 10:00:01 2025 UTC |
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.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?OK, well can you check to see that microtime() is working for you? This script should print out 100. <?php $a = array(); for($i=0; $i<100; $i++) { $a[((double)microtime()*1000000)]= 1; } echo count($a), "\n"; ?> BTW, your most recent example (calling a "remote" script) is essentially the same as calling the local function pwd(). You still are re-seeding the PRNG with each time you call the function. In order to produce a pseudo-random sequence you should only seed the PRNG once. This is a hard thing to do in the case where you generate one random number from the sequence per execution. Here is something that works for me even though I call the srand() function inside the function (though the randomness is probably pretty poor). You might be able to adapt it in some way for your use. The idea here is that once you shuffle the array once, you use that as the starting point for the next shuffle. <?php $pass_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); function pwd(&$pass_array) { srand ((double)microtime()*1000000); $password = ''; shuffle(&$pass_array); for ($i=0; $i<6; $i++) { $password .= $pass_array[$i]; } return $password; } $a = array(); for($i=0; $i<500; $i++) { $a[pwd($pass_array)] = 1; } echo count($a), "\n"; This is not a *bug* (IMHO). This is a problem with your script, you might want to try this discussion on php-general@lists.php.net and see if people have more suggestions for you.