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>"; }
相关文章推荐
- PHP生成随机数的方法总结
- 生成PHP随机数的几种方法
- PHP n个不重复的随机数生成代码
- php生成随机数 自定义函数 randstr($length)
- PHP生成随机数的方法实例分析
- PHP随机数生成代码与使用实例分析
- PHP实现加权随机数(Weighted Random sampling)的生成算法
- php生成N个不重复的随机数实例
- PHP 生成一定数量的不重复随机数
- php简单生成随机数的方法
- 用PHP生成随机数的函数
- PHP之生成一个随机数
- php 生成 N位随机数,并且加上MD5加密
- php生成随机数:自定义函数 randstr($length)
- PHP不重复随机数的生成方法
- 生成php随机数
- PHP基于自增数据如何生成不重复的随机数示例
- 求一个生成四位随机数的PHP代码
- PHP随机数生成与使用解析