您的位置:首页 > 编程语言 > PHP开发

php生成随机数

2012-04-06 09:30 309 查看
// 生成0123456789abcdefghijklmnopqrstuvwxyz中的一个字符
function getOptions()
{
$options = array();
$result = array();
for($i=48; $i<=57; $i++)
{
array_push($options,chr($i));
}
for($i=65; $i<=90; $i++)
{
$j = 32;
$small = $i + $j;
array_push($options,chr($small));
}
return $options;
}

$e = getOptions();
for($j=0; $j<150; $j++)
{
echo $e[$j];
}

echo "<hr>";
$len = 10;

// 随机生成数组索引,从而实现随机数 用j来控制生成的随机数的个数
for($j=0;$j<10; $j++)
{
$result = "";
$options = getOptions();
$lastIndex = 35;
while (strlen($result)<$len)
{
// 从0到35中随机取一个作为索引
$index = rand(0,$lastIndex);
// 将随机数赋给变量 $chr
$chr = $options[$index];
// 随机数作为 $result 的一部分
$result .= $chr;
$lastIndex = $lastIndex-1;
// 最后一个索引将不会参与下一次随机抽奖
$options[$index] = $options[$lastIndex];
}
echo $j."--".$result."<br>";
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: