rand() function

From: Date: Fri, 07 Jul 2000 00:30:55 +0000
Subject: rand() function
Groups: php.general 
Request: Send a blank email to php-general+get-5291@lists.php.net to get a copy of this message
How random is it? Here's the whole story: I'm learning PHP and fooling around making random useless scripts... the latest one generates 3 random numbers (64-91) translates those to ASC II values with Chr() and then makes them into a string. If that string == "CAR" the loop ends. meanwhile it counts how many tries it took to get that random combination. I've also had it record the number of tries. I thought it should be very different, maybe one identical number, but here are the results: 908 1254 1254 1254 1254 1254 2201 3504 4266 4749 4749 4749 4749 4749 4749 5967 5967 6005 6005 6005 6796 9935 11217 14186 16265 19015 21840 21840 23006 23549 28975 28975 28975 28975 30283 30283 38056 39374 50252 50252 50252 50252 50252 85957 You will notice that a huge amount of them are identical... this isnt some recording bug, i started recording because i saw a lot of the numbers being the same.... so... uhm, why does this happen? It shouldnt get it on the nth time so often if it is random... if you think its a bug in my code, here it is: the main variable, $cat is the string with the 3 random chars. very inappropriately named, I know. <?php $cat = ""; $count = 0; while($cat != "CAR") { $i = rand(64,91); $j = rand(64,91); $k = rand(64,91); $c = Chr($i); $d = Chr($j); $e = Chr($k); $count++; echo $count . "<br>"; $cat = $c . $d . $e; } echo $cat; $fp = fopen("counts.num","a-"); fputs($fp, $count . "\n"); fclose($fp); ?> Thanks Ed (sorry for the long post, if you read through all of it, thanks again for your interest

« previous php.general (#5291) next »