第一种方法用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', '!', 
 '@','#', '$', '%', '^', '&', '*', '(', ')', '-', '_', 
 '[', ']', '{', '}', '<', '>', '~', '`', '+', '=', ',', 
 '.', ';', ':', '/', '"htmlcode">
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); 

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

lcg_value说明

float lcg_value ( void )

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

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

<"color: #ff0000">两种生成0~1随机小数方法进行比较

1.执行时间比较

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

<"run time %f ms\r\n", ($endtime-$starttime)*1000);
"htmlcode">
<"run time %f ms\r\n", ($endtime-$starttime)*1000);
"htmlcode">
<"htmlcode">
<?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);
?>

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。