首頁  >  文章  >  後端開發  >  PHP有哪些產生隨機數的方法

PHP有哪些產生隨機數的方法

php中世界最好的语言
php中世界最好的语言原創
2018-04-11 09:17:381453瀏覽

這次帶給大家PHP有哪些產生隨機數的方法,PHP產生隨機數的注意事項有哪些,下面就是實戰案例,一起來看一下。

# 第一種方法用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(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;, &#39;h&#39;, 
 &#39;i&#39;, &#39;j&#39;, &#39;k&#39;, &#39;l&#39;,&#39;m&#39;, &#39;n&#39;, &#39;o&#39;, &#39;p&#39;, &#39;q&#39;, &#39;r&#39;, &#39;s&#39;, 
 &#39;t&#39;, &#39;u&#39;, &#39;v&#39;, &#39;w&#39;, &#39;x&#39;, &#39;y&#39;,&#39;z&#39;, &#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, 
 &#39;E&#39;, &#39;F&#39;, &#39;G&#39;, &#39;H&#39;, &#39;I&#39;, &#39;J&#39;, &#39;K&#39;, &#39;L&#39;,&#39;M&#39;, &#39;N&#39;, &#39;O&#39;, 
 &#39;P&#39;, &#39;Q&#39;, &#39;R&#39;, &#39;S&#39;, &#39;T&#39;, &#39;U&#39;, &#39;V&#39;, &#39;W&#39;, &#39;X&#39;, &#39;Y&#39;,&#39;Z&#39;, 
 &#39;0&#39;, &#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;, &#39;6&#39;, &#39;7&#39;, &#39;8&#39;, &#39;9&#39;, &#39;!&#39;, 
 &#39;@&#39;,&#39;#&#39;, &#39;$&#39;, &#39;%&#39;, &#39;^&#39;, &#39;&&#39;, &#39;*&#39;, &#39;(&#39;, &#39;)&#39;, &#39;-&#39;, &#39;_&#39;, 
 &#39;[&#39;, &#39;]&#39;, &#39;{&#39;, &#39;}&#39;, &#39;<&#39;, &#39;>', '~', '`', '+', '=', ',', 
 '.', ';', ':', '/', '?', '|'); 
 // 在 $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=&#39;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890&#39;; 
$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) 的偽隨機數。

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

輸出:

0.11516515851995
0.064684551575297
0.68275174031189
0.55730746529099
0.7021500878021501

兩種產生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(&#39; &#39;, 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(&#39; &#39;, 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(&#39;content-type: image/png&#39;);
$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(&#39;content-type: image/png&#39;);
$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中文網其它相關文章!

推薦閱讀:

Anaconda的新手使用大全

在win裡anaconda怎麼安裝第三方的函式庫

以上是PHP有哪些產生隨機數的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn