$list_ar = array();
for($x = 0;$x < 500; $x++){
$val = generateRandomString(20);
if(!in_array($val,$list_ar)){
echo $x.'=='.$val.'<br>';
array_push($list_ar,$val);
} else {
echo $x.'== IN ARRAY<br>';
}
}
function generateRandomString($length){
$characterlist = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789';
$characterlist_array = str_split($characterlist);
$id = '';
for($a = 0;$a<$length;$a++){
shuffle($characterlist_array);
$position = array_rand($characterlist_array, 1);
$id .= $characterlist_array[$position];
}
return $id;
}
When I run the code above it gives in array false up to 360 lines after that it returns in array true. I'm expecting it to return in array false up to 500 lines I've been running the code more than 20 times and it gives the same result of exactly 360 lines in array false. Any ideas?
$id .= $characterlist[rand(0,count($characterlist)-1)]than is to completely shuffle the array and then generate a random number to generate the key.in arrayany times: codepad.org/7kRUTLRyarray_randhere php.net/manual/en/function.array-rand.php#105265 That indicates that it has very strange randomness behavior (like, it might not actually be particularly random at all). I tried a couple of different ideas, and as long as I don't call that function, I can make this work.