I always thought my code generated pretty random strings but I've been pressing F5 for about 10 minutes and I display 10 strings at once and I have had THREE DUPLICATES, UNIBON, ZANOPE and ZOTAXS.
Can anyone explain why this is when I though there code be 26^6 possibilities?
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$pass = '';
for ($i = 0; $i < $len; $i++){
$pass .= $chars[(rand() % strlen($chars))];
}
return $pass;
Any advice would be much appreciated.
Thanks
Using mt_rand the first duplicate takes on average between 10 and 60 seconds, that seems okay doesn't it?
echo 'start: '.date('H:i:s');
for ($i = 1; ; $i++) {
$testarr[] = passGen(6);
$new = passGen(6);
if (in_array($new,$testarr)){
echo '<br>end: '.date('H:i:s');
echo '<br>string: '.$new;
echo '<br>count: '.count($testarr);
break;
}
}
rand(0,strlen($chars))for ($i = 0; $i < $len; $i++){where do you define$len?mt_rand()to get a better random number asrand()is known to have problems per the PHP manual. see (php.net/manual/en/function.mt-rand.php)