PHP generates a random password function, which can be used to generate a random number or generate a random password. if you need a random password, refer to it. the specific function code is as follows: PHP generates random number password function (six by default) functionrandStr ($ len6, $ formatALL) {switch ($ format) {caseALL: $ charsABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnop PHP generates random password function, this function can be used to generate a random number or a random password. For more information, see function code:
// PHP random number generation and password function (six digits by default)
function randStr($len=6,$format='ALL'){
switch($format){
case 'ALL':
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~';
break;
case 'CHAR':
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-@#~';
break;
case 'NUMBER':
$chars='0123456789';
break;
default :
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~';
break;
}
mt_srand((double)microtime()*1000000*getmypid());
$password='';
while(strlen($password)<$len){
$password.=substr($chars,(mt_rand()%strlen($chars)),1);
}
return $password;
}
Several methods for generating random numbers in php are summarized as follows:
The first method is very simple. you can directly use the built-in functions:
Srand (double) microtime () * 1000000 );
// Randomly generate an integer between 0 and 99
$ Randval = rand (0,99999999 );
Echo $ randval ,'
';
/* Similar output: 32659912 */
The second method is a little more complex. it not only generates random strings with only numbers, but also contains various special characters:
function randomkeys($length){
$output='';
for($a=0;$a<$length; $a++){
$output.=chr(mt_rand(33, 126));
}
return $output;
}
echo randomkeys(20);
/* Similar output: EGztLufY \ Eq/br {y!;> P */