php生成随机数

来源:互联网 发布:怎样使淘宝排名靠前 编辑:程序博客网 时间:2024/06/07 03:07
// 生成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>";}


原创粉丝点击