时间:2021-07-01 10:21:17 帮助过:2人阅读
public 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;
}
随机数生成结果总是一样 在阿里云上
在本地没问题
public 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;
}
建议不要使用mt_srand((double) microtime() * 1000000 * getmypid());
播撒随机种子,现在这个功能已经能自动完成了,另外你播撒的这个值就是固定的。
代码没问题,我在阿里云服务器与本地都测试过了。seed 相同的几率已经很低了。
php7使用random-int