/**
* 产生随机数
* @param type $length
* @param type $code_type (1:数字,2:大写字母和数字,3:小写字母和数字,4:大写字母,5:小写字母,其他:大小写字母和数字
* @return string
*/
function random($length, $code_type = 0) {
mt_srand((double) microtime() * 1000000);
if ($code_type == 1) {
$hash = sprintf('%0' . $length . 'd', mt_rand(0, pow(10, $length) - 1));
} else {
$hash = '';
$chars = '';
if ($code_type == 2){
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
}elseif ($code_type == 3){
$chars = '0123456789abcdefghijklmnopqrstuvwxyz';
}elseif ($code_type == 4){
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
}elseif ($code_type == 5){
$chars = 'abcdefghijklmnopqrstuvwxyz';
}else{
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
}
$max = strlen($chars) - 1;
for ($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
}
return $hash;
}
产生随机数
最新推荐文章于 2024-05-15 00:17:38 发布