// 生成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>";
}
php生成随机数
最新推荐文章于 2021-03-29 16:45:01 发布