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

主頁 > 知識庫 > PHP生成隨機(jī)數(shù)的方法總結(jié)

PHP生成隨機(jī)數(shù)的方法總結(jié)

熱門標(biāo)簽:長沙crm外呼系統(tǒng)業(yè)務(wù) 南寧高頻外呼回?fù)芟到y(tǒng)哪家好 400電話申請方法收費 電話機(jī)器人危險嗎 江蘇外呼電銷機(jī)器人報價 離石地圖標(biāo)注 深圳外呼系統(tǒng)收費 專業(yè)電話機(jī)器人批發(fā)商 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 中隨機(jī)取 $length 個數(shù)組元素鍵名 
 $keys = array_rand($chars, $length); 
 $password = ''; 
 for($i = 0; $i  $length; $i++) 
 { 
  // 將 $length 個數(shù)組元素連接成字符串 
  $password .= $chars[$keys[$i]]; 
 } 
 return $password; 
} 

第三種取當(dāng)時時間戳

function get_password( $length = 8 ) 
{ 
 $str = substr(md5(time()), 0, $length);//md5加密,time()當(dāng)前時間戳 
 return $str; 
} 

第四種打亂字符串

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

 //開始創(chuàng)建驗證碼(直接用函數(shù)生成,比較方便快捷)

$code = rand(10000, 99999); 

php mt_rand生成0~1隨機(jī)小數(shù)的效果比較

lcg_value說明

float lcg_value ( void )

lcg_value() 返回范圍為 (0, 1) 的一個偽隨機(jī)數(shù)。本函數(shù)組合了周期為 2^31 - 85 和 2^31 - 249 的兩個同余發(fā)生器。本函數(shù)的周期等于這兩個素數(shù)的乘積。

返回:范圍為 (0, 1) 的偽隨機(jī)數(shù)。

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

輸出:

0.11516515851995
0.064684551575297
0.68275174031189
0.55730746529099
0.70215008878091

兩種生成0~1隨機(jī)小數(shù)方法進(jìn)行比較

1.執(zhí)行時間比較

執(zhí)行10萬次基于mt_rand()與mt_getrandmax()算法的運行時間

?php
/**
 * 生成0~1隨機(jī)小數(shù)
 * @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();

// 執(zhí)行10萬次獲取隨機(jī)小數(shù)
for($i=0; $i100000; $i++){
 randFloat();
}

// 記錄結(jié)束時間
$endtime = get_microtime();

// 輸出運行時間
printf("run time %f ms\r\n", ($endtime-$starttime)*1000);
?>

輸出:run time 266.893148 ms

執(zhí)行10萬次lcg_value()的運行時間

?php
// 獲取microtime
function get_microtime(){
 list($usec, $sec) = explode(' ', microtime());
 return (float)$usec + (float)$sec;
}


// 記錄開始時間
$starttime = get_microtime();


// 執(zhí)行10萬次獲取隨機(jī)小數(shù)
for($i=0; $i100000; $i++){
 lcg_value();
}


// 記錄結(jié)束時間
$endtime = get_microtime();


// 輸出運行時間
printf("run time %f ms\r\n", ($endtime-$starttime)*1000);
?>

輸出:run time 86.178064 ms

執(zhí)行時間上比較,因為lcg_value()直接是php原生方法,而mt_rand()與mt_getrandmax()需要調(diào)用兩個方法,并需要進(jìn)行計算,因此lcg_value()的執(zhí)行時間大約快3倍。

2.隨機(jī)效果比較

基于mt_rand()與mt_getrandmax()算法的隨機(jī)效果

?php
/**
 * 生成0~1隨機(jī)小數(shù)
 * @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()的隨機(jī)效果

?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);
?>

您可能感興趣的文章:
  • PHP實現(xiàn)隨機(jī)數(shù)字、字母的驗證碼功能
  • 深入理解PHP中mt_rand()隨機(jī)數(shù)的安全
  • PHP簡單獲取隨機(jī)數(shù)的常用方法小結(jié)
  • PHP獲取redis里不存在的6位隨機(jī)數(shù)應(yīng)用示例【設(shè)置24小時過時】
  • PHP基于自增數(shù)據(jù)如何生成不重復(fù)的隨機(jī)數(shù)示例
  • PHP內(nèi)置函數(shù)生成隨機(jī)數(shù)實例

標(biāo)簽:南京 興安盟 株洲 南昌 白酒營銷 濱州 曲靖 太原

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP生成隨機(jī)數(shù)的方法總結(jié)》,本文關(guān)鍵詞  PHP,生成,隨機(jī)數(shù),的,方法,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《PHP生成隨機(jī)數(shù)的方法總結(jié)》相關(guān)的同類信息!
  • 本頁收集關(guān)于PHP生成隨機(jī)數(shù)的方法總結(jié)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 建平县| 福鼎市| 竹北市| 都安| 甘德县| 英吉沙县| 碌曲县| 宝鸡市| 通渭县| 浦江县| 育儿| 江都市| 肥乡县| 韶关市| 夏邑县| 武穴市| 岚皋县| 肇庆市| 高淳县| 博客| 永春县| 崇文区| 昂仁县| 四会市| 景泰县| 荃湾区| 丰镇市| 哈尔滨市| 师宗县| 英吉沙县| 田林县| 灵武市| 三门县| 辛集市| 潢川县| 宝兴县| 靖江市| 平度市| 广宗县| 英吉沙县| 河间市|