国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP生成隨機數的方法總結

2024-09-04 11:49:28
字體:
來源:轉載
供稿:網友

第一種方法用mt_rand():

  1. function GetRandStr($length){  
  2. $str='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';  
  3. $len=strlen($str)-1;  
  4. $randstr='';  
  5. for($i=0;$i<$length;$i++){  
  6. $num=mt_rand(0,$len);  
  7. $randstr .= $str[$num];  
  8. }  
  9. return $randstr;  
  10. }  
  11. $number=GetRandStr(6);  
  12. echo $number

第二種方法(最快的):

  1. function make_password( $length = 8 )  
  2. {  
  3.  // 密碼字符集,可任意添加你需要的字符  
  4.  $chars = array('a''b''c''d''e''f''g''h',  
  5.  'i''j''k''l','m''n''o''p''q''r''s',  
  6.  't''u''v''w''x''y','z''A''B''C''D',  
  7.  'E''F''G''H''I''J''K''L','M''N''O',  
  8.  'P''Q''R''S''T''U''V''W''X''Y','Z',  
  9.  '0''1''2''3''4''5''6''7''8''9''!',  
  10.  '@','#''$''%''^''&''*''('')''-''_',  
  11.  '['']''{''}''<''>''~''`''+''='',',  
  12.  '.'';'':''/''?''|');  
  13.  // 在 $chars 中隨機取 $length 個數組元素鍵名  
  14.  $keys = array_rand($chars$length);  
  15.  $password = '';  
  16.  for($i = 0; $i < $length$i++)  
  17.  {  
  18.   // 將 $length 個數組元素連接成字符串  
  19.   $password .= $chars[$keys[$i]];  
  20.  } //Vevb.com 
  21.  return $password;  

第三種取當時時間戳:

  1. function get_password( $length = 8 )  
  2. {  
  3.  $str = substr(md5(time()), 0, $length);//md5加密,time()當前時間戳  
  4.  return $str;  

第四種打亂字符串:

  1. function getrandstr(){  
  2. $str='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';  
  3. $randStr = str_shuffle($str);//打亂字符串  
  4. $randssubstr($randStr,0,6);//substr(string,start,length);返回字符串的一部分  
  5. 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) 的偽隨機數。

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

輸出:

0.11516515851995

0.064684551575297

0.68275174031189

0.55730746529099

0.70215008878091

兩種生成0~1隨機小數方法進行比較

1.執行時間比較

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

  1. <?php 
  2. /** 
  3.  * 生成0~1隨機小數 
  4.  * @param Int $min 
  5.  * @param Int $max 
  6.  * @return Float 
  7.  */ 
  8. function randFloat($min=0, $max=1){ 
  9.  return $min + mt_rand()/mt_getrandmax() * ($max-$min); 
  10.   
  11. // 獲取microtime 
  12. function get_microtime(){ 
  13.  list($usec$sec) = explode(' ', microtime()); 
  14.  return (float)$usec + (float)$sec
  15.   
  16. // 記錄開始時間 
  17. $starttime = get_microtime(); 
  18.   
  19. // 執行10萬次獲取隨機小數 
  20. for($i=0; $i<100000; $i++){ 
  21.  randFloat(); 
  22. //Vevb.com 
  23.   
  24. // 記錄結束時間 
  25. $endtime = get_microtime(); 
  26.   
  27. // 輸出運行時間 
  28. printf("run time %f ms/r/n", ($endtime-$starttime)*1000); 
  29. ?> 

輸出:run time 266.893148 ms

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

  1. <?php 
  2. // 獲取microtime 
  3. function get_microtime(){ 
  4.  list($usec$sec) = explode(' ', microtime()); 
  5.  return (float)$usec + (float)$sec
  6.  
  7. // 記錄開始時間 
  8. $starttime = get_microtime(); 
  9.   
  10. // 執行10萬次獲取隨機小數 
  11. for($i=0; $i<100000; $i++){ 
  12.  lcg_value(); 
  13.  
  14. // 記錄結束時間 
  15. $endtime = get_microtime(); 
  16.   
  17. // 輸出運行時間 
  18. printf("run time %f ms/r/n", ($endtime-$starttime)*1000); 
  19. ?> 

輸出:run time 86.178064 ms

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

2.隨機效果比較

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

  1. <?php 
  2. /** 
  3.  * 生成0~1隨機小數 
  4.  * @param Int $min 
  5.  * @param Int $max 
  6.  * @return Float 
  7.  */ 
  8. function randFloat($min=0, $max=1){ 
  9.  return $min + mt_rand()/mt_getrandmax() * ($max-$min); 
  10.   
  11.   
  12. header('content-type: image/png'); 
  13. $im = imagecreatetruecolor(512, 512); 
  14. $color1 = imagecolorallocate($im, 255, 255, 255); 
  15. $color2 = imagecolorallocate($im, 0, 0, 0); 
  16. for($y=0; $y<512; $y++){ 
  17.  for($x=0; $x<512; $x++){ 
  18.   $rand = randFloat(); 
  19.   if(round($rand,2)>=0.5){ 
  20.    imagesetpixel($im$x$y$color1); 
  21.   }else
  22.    imagesetpixel($im$x$y$color2); 
  23.   } 
  24.  } 
  25. imagepng($im); 
  26. imagedestroy($im); 
  27. ?> 

lcg_value()的隨機效果

  1. <?php 
  2. header('content-type: image/png'); 
  3. $im = imagecreatetruecolor(512, 512); 
  4. $color1 = imagecolorallocate($im, 255, 255, 255); 
  5. $color2 = imagecolorallocate($im, 0, 0, 0); 
  6. for($y=0; $y<512; $y++){ 
  7.  for($x=0; $x<512; $x++){ 
  8.   $rand = lcg_value(); 
  9.   if(round($rand,2)>=0.5){ 
  10.    imagesetpixel($im$x$y$color1); 
  11.   }else
  12.    imagesetpixel($im$x$y$color2); 
  13.   } 
  14.  } 
  15. imagepng($im); 
  16. imagedestroy($im); 
  17. ?>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 开化县| 武夷山市| 寿光市| 澄城县| 潞城市| 乌拉特后旗| 额济纳旗| 林周县| 同心县| 读书| 黄冈市| 和静县| 宽城| 珲春市| 巢湖市| 万宁市| 临沂市| 庆安县| 古交市| 梁平县| 安远县| 鄂尔多斯市| 衡阳县| 广宗县| 玉龙| 沂南县| 太康县| 和政县| 兴隆县| 梅河口市| 新建县| 高唐县| 夏河县| 洪雅县| 景宁| 临潭县| 东明县| 通榆县| 永州市| 德江县| 巴楚县|