当前位置: 首页 > news >正文

php如何设置随机数

  • 第一种方法用mt_rand()

  • 第二种方法(最快的)

  • 第三种取当时时间戳

  • 第四种打乱字符串

  • 5 //开始创建验证码(直接用函数生成,比较方便快捷)

  • php mt_rand生成0~1随机小数的效果比较

第一种方法用mt_rand()

function GetRandStr($length){
$str='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$len=strlen($str)-1;
$randstr='';
for($i=0;$i<$length;$i++){
$num=mt_rand(0,$len);
$randstr .= $str[$num];
}
return $randstr;
}
$number=GetRandStr(6);
echo $number;

第二种方法(最快的)

function make_password( $length = 8 )
{// 密码字符集,可任意添加你需要的字符$chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y','z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L','M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y','Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!', '@','#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '[', ']', '{', '}', '<', '>', '~', '`', '+', '=', ',', '.', ';', ':', '/', '?', '|');// 在 $chars 中随机取 $length 个数组元素键名$keys = array_rand($chars, $length); $password = '';for($i = 0; $i < $length; $i++){// 将 $length 个数组元素连接成字符串$password .= $chars[$keys[$i]];}return $password;
}

 第三种取当时时间戳

function get_password( $length = 8 ) 
{$str = substr(md5(time()), 0, $length);//md5加密,time()当前时间戳return $str;
}

第四种打乱字符串

function getrandstr(){
$str='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
$randStr = str_shuffle($str);//打乱字符串
$rands= substr($randStr,0,6);//substr(string,start,length);返回字符串的一部分
return $rands;
}

 5 //开始创建验证码(直接用函数生成,比较方便快捷)

$code = rand(10000, 99999);

php mt_rand生成0~1随机小数的效果比较

lcg_value说明

float lcg_value ( void )
lcg_value() 返回范围为 (0, 1) 的一个伪随机数。本函数组合了周期为 2^31 - 85 和 2^31 - 249 的两个同余发生器。本函数的周期等于这两个素数的乘积。

返回:范围为 (0, 1) 的伪随机数。

<?php
for($i=0; $i<5; $i++){echo lcg_value().PHP_EOL;
}
?>

输出:

0.11516515851995
0.064684551575297
0.68275174031189
0.55730746529099
0.70215008878091

两种生成0~1随机小数方法进行比较

1.执行时间比较

执行10万次基于mt_rand()与mt_getrandmax()算法的运行时间

<?php
/*** 生成0~1随机小数* @param  Int   $min* @param  Int   $max* @return Float*/
function randFloat($min=0, $max=1){return $min + mt_rand()/mt_getrandmax() * ($max-$min);
}// 获取microtime
function get_microtime(){list($usec, $sec) = explode(' ', microtime());return (float)$usec + (float)$sec;
}// 记录开始时间
$starttime = get_microtime();// 执行10万次获取随机小数
for($i=0; $i<100000; $i++){randFloat();
}// 记录结束时间
$endtime = get_microtime();// 输出运行时间
printf("run time %f ms\r\n", ($endtime-$starttime)*1000);
?>

输出:run time 266.893148 ms

执行10万次lcg_value()的运行时间

<?php
// 获取microtime
function get_microtime(){list($usec, $sec) = explode(' ', microtime());return (float)$usec + (float)$sec;
}// 记录开始时间
$starttime = get_microtime();// 执行10万次获取随机小数
for($i=0; $i<100000; $i++){lcg_value();
}// 记录结束时间
$endtime = get_microtime();// 输出运行时间
printf("run time %f ms\r\n", ($endtime-$starttime)*1000);
?>

输出:run time 86.178064 ms

执行时间上比较,因为lcg_value()直接是php原生方法,而mt_rand()与mt_getrandmax()需要调用两个方法,并需要进行计算,因此lcg_value()的执行时间大约快3倍。

2.随机效果比较

基于mt_rand()与mt_getrandmax()算法的随机效果

<?php
/*** 生成0~1随机小数* @param  Int   $min* @param  Int   $max* @return Float*/
function randFloat($min=0, $max=1){return $min + mt_rand()/mt_getrandmax() * ($max-$min);
}header('content-type: image/png');
$im = imagecreatetruecolor(512, 512);
$color1 = imagecolorallocate($im, 255, 255, 255);
$color2 = imagecolorallocate($im, 0, 0, 0);
for($y=0; $y<512; $y++){for($x=0; $x<512; $x++){$rand = randFloat();if(round($rand,2)>=0.5){imagesetpixel($im, $x, $y, $color1);}else{imagesetpixel($im, $x, $y, $color2);}}
}
imagepng($im);
imagedestroy($im);
?>

lcg_value()的随机效果

<?php
header('content-type: image/png');
$im = imagecreatetruecolor(512, 512);
$color1 = imagecolorallocate($im, 255, 255, 255);
$color2 = imagecolorallocate($im, 0, 0, 0);
for($y=0; $y<512; $y++){for($x=0; $x<512; $x++){$rand = lcg_value();if(round($rand,2)>=0.5){imagesetpixel($im, $x, $y, $color1);}else{imagesetpixel($im, $x, $y, $color2);}}
}
imagepng($im);
imagedestroy($im);
?>

 以上是“php如何设置随机数”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助.

http://www.lryc.cn/news/162485.html

相关文章:

  • WebGL 纹理——在矩形表面贴上图像
  • javaweb-Filter-listener过滤器与监听器
  • DBeaver 常用快捷键【大全】
  • 数据分析之面试题目汇总
  • 【Vue-Element-Admin】级联查询
  • 深入探讨Kubernetes(K8s)在云原生架构中的关键作用和应用
  • redis zset score 求和
  • springboot属性注入增强(一)背景/需求
  • 《PWA实战:如何为你的网站增加离线功能和推送通知》
  • sqli-labs关卡之一(两种做法)
  • Visual Studio 线性表的链式存储节点输出引发异常:读取访问权限冲突
  • [通用]计算机经典面试题基础篇Day3
  • (Golang) 牛客 在线编程 Go语言入门
  • 2.6 PE结构:导出表详细解析
  • SpringMvc进阶
  • SpringCloud Alibaba 入门到精通 - Nacos
  • new/delete, malloc/free
  • oracle将一个用户的表复制到另一个用户
  • C#知识点、常见面试题
  • 【STM32】锁存器
  • DGIOT-Modbus-RTU控制指令05、06的配置与下发
  • 机器学习实战-系列教程8:SVM分类实战3非线性SVM(鸢尾花数据集/软间隔/线性SVM/非线性SVM/scikit-learn框架)项目实战、代码解读
  • 计算机网络-谢希任第八版学习笔记总结
  • 手写Spring:第5章-注入属性和依赖对象
  • 初识集合框架 -Java
  • 目标检测笔记(十五): 使用YOLOX完成对图像的目标检测任务(从数据准备到训练测试部署的完整流程)
  • 深眸科技自研轻辙视觉引擎,以AI机器视觉赋能杆号牌识别与分拣
  • Shell命令管理进程
  • python创建exe文件
  • 【数据结构】AVL树的插入与验证