PHP生成随机数有多少方法,哪种好呢
Admin 2022-09-14 群英技术资讯 1145 次浏览
在日常操作或是项目的实际应用中,有不少朋友对于“PHP生成随机数有多少方法,哪种好呢”的问题会存在疑惑,下面小编给大家整理和分享了相关知识和资料,易于大家学习和理解,有需要的朋友可以借鉴参考,下面我们一起来了解一下吧。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;
}
$code = rand(10000, 99999);
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随机小数方法进行比较
执行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倍。
基于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生成随机数有多少方法,哪种好呢”的学习就结束了,希望能够解决大家的疑惑,另外大家动手实践也很重要,对大家加深理解和学习很有帮助。如果想要学习更多的相关知识,欢迎关注群英网络资讯站,小编每天都会给大家分享实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
很多时候我们需要将几个字符串连接起来显示,在PHP中,字符串之间使用“点”来连接,也就是英文中的半角句号 "." 。"."是字符串连接符,可以把两个或两个以上的字符串链接成一个字符串。例如: 12345<?php$name = "PHP中文网:";$url = "www.php";echo $name . $url . ".cn";
PHP中实现输出偶奇数的方法及代码是什么,下文有实例供大家参考,对大家了解操作过程或相关知识有一定的帮助,而且实用性强,希望这篇文章能帮助大家,下面我们一起来了解看看吧。
这篇文章给大家分享的是有关php数组函数的内容,主要介绍array_push()、array_pop()及array_shift()对数组进行入栈,出栈及移除的操作,小编觉得挺实用的,因此分享给大家做个参考,感兴趣的朋友就继续往下看吧。
php桥接模式的介绍:1、将两个原本不相关的类结合在一起,然后利用两个类中的方法和属性,输出一份新的结果。2、结构分为抽象类、被提炼的抽象类、实现类、具体实现类和客户端代码。
在本篇文章里小编给大家整理了一篇关于php substr()去掉最后一位字符的实例方法,有兴趣的朋友们可以跟着学习下。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008