php 随机数 随机位数,请帮忙, 谢谢!先谢谢大家进来帮忙!现有如下随机函数.生成随机数.function random($length, $numeric = 0) { PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000); if ($numeric) { $hash = spri

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/12 11:44:13
php 随机数 随机位数,请帮忙, 谢谢!先谢谢大家进来帮忙!现有如下随机函数.生成随机数.function random($length, $numeric = 0) { PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000); if ($numeric) {  $hash = spri

php 随机数 随机位数,请帮忙, 谢谢!先谢谢大家进来帮忙!现有如下随机函数.生成随机数.function random($length, $numeric = 0) { PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000); if ($numeric) { $hash = spri
php 随机数 随机位数,请帮忙, 谢谢!
先谢谢大家进来帮忙!
现有如下随机函数.生成随机数.
function random($length, $numeric = 0) {
PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
if ($numeric) {
$hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
} else {
$hash = '';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
for ($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
}
return $hash;
}
如果我用 $str = random(4);
来设置随机位数为4.
那么我想请大家帮我看看如何实现:
随机数的随机位数,
也就是 如何实现 $str 有时等于 random(4) 有时等于 random(3)
也就是说随机位数2-4位数
谢谢了!

php 随机数 随机位数,请帮忙, 谢谢!先谢谢大家进来帮忙!现有如下随机函数.生成随机数.function random($length, $numeric = 0) { PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000); if ($numeric) { $hash = spri
调用函数时用 $str = random();
function random($length,$numeric = 0) {
if (!isset($length)) $length = rand(2,4);
//加入$length=rand(2,4)就可以了
PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
if ($numeric) {
$hash = sprintf('%0'.$length.'d',mt_rand(0,pow(10,$length) - 1));
} else {
$hash = '';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
for ($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0,$max)];
}
}
return $hash;
}
Adam
www.61dh.com/blog