Making random string function function more random?

From: Date: Fri, 06 Dec 2002 08:32:29 +0000
Subject: Making random string function function more random?
Groups: php.general 
Request: Send a blank email to php-general+get-127230@lists.php.net to get a copy of this message
I'm using the following function to generate a random string: function randstring($minasc = 32,$maxasc = 255,$minlength = 0,$maxlength = 0){ //If maximum length is lower than minimum length, return string representation of false if($maxlength != 0 and $minlength != 0 and $minlength > $maxlength){
       return '';
} //Initialize $return $return = ''; //Start infinite while loop, which will be exited with break; while(true){
       //Add another random character to $return
       $return = $return.chr(mt_rand($minasc,$maxasc));
       //Break out of loop when it's time to
       if((strlen($return) == $maxlength) or (mt_rand(1,2) == 2 and (strlen($return) >= $minlength or $minlength == 0) and (strlen($return) <= $maxlength or $maxlength == 0))){
           break;
       }
} //Return the value of $return return $return; } But when I run it with: randstring(65,90,4,4); and I check how many strings it generates before reaching a certain string, some strings take much longer than others. Is this just a problem with using pseudo-random numbers, or is there a better way to randomize this function? -- The above message is encrypted with double rot13 encoding. Any unauthorized attempt to decrypt it will be prosecuted to the full extent of the law.

« previous php.general (#127230) next »