婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > 如何在PHP中生成隨機數

如何在PHP中生成隨機數

熱門標簽:天客通地圖標注 公司外呼系統中心 菏澤語音外呼系統運營商 如何在世界地圖標注 臨沂crm外呼系統平臺 電子地圖標注怎么修改 地圖標注符號樣式有 廈門400電話辦理選易號網 梧州市機器人外呼系統怎么樣

第一種方法用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;
}

第五種開始創建驗證碼(直接用函數生成,比較方便快捷)

$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; $i5; $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; $i100000; $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; $i100000; $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; $y512; $y++){
 for($x=0; $x512; $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; $y512; $y++){
 for($x=0; $x512; $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);
?>

3.隨機閱讀推薦

+ php給源碼加密的方法總結:https://www.jb51.net/article/134506.html

以上就是如何在PHP中生成隨機數的詳細內容,更多關于PHP隨機數的資料請關注腳本之家其它相關文章!

您可能感興趣的文章:
  • PHP生成指定范圍內的N個不重復的隨機數
  • PHP內置函數生成隨機數實例
  • php簡單生成隨機數的方法
  • php生成不重復隨機數、數組的4種方法分享
  • PHP生成不重復隨機數的方法匯總
  • php生成隨機數的三種方法
  • PHP生成指定長度隨機數最簡潔的方法
  • 在PHP模板引擎smarty生成隨機數的方法和math函數詳解

標簽:貴陽 白城 雞西 郴州 迪慶 瀘州 黃石 綿陽

巨人網絡通訊聲明:本文標題《如何在PHP中生成隨機數》,本文關鍵詞  如,何在,PHP,中,生成,隨機數,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《如何在PHP中生成隨機數》相關的同類信息!
  • 本頁收集關于如何在PHP中生成隨機數的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 咸丰县| 潼南县| 枞阳县| 黎川县| 瓦房店市| 长武县| 宁蒗| 浙江省| 三明市| 广昌县| 康平县| 城固县| 江永县| 武宁县| 洮南市| 凌海市| 巴楚县| 仪陇县| 临清市| 进贤县| 琼结县| 库尔勒市| 东宁县| 余干县| 临沭县| 乐东| 时尚| 台湾省| 昭觉县| 府谷县| 谷城县| 集贤县| 德惠市| 通州市| 民乐县| 威海市| 屯门区| 阿拉善盟| 密云县| 于都县| 札达县|