如何在PHP中生成隨機(jī)數(shù)
第一種方法用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 個(gè)數(shù)組元素鍵名
$keys = array_rand($chars, $length);
$password = '';
for($i = 0; $i < $length; $i++)
{
// 將 $length 個(gè)數(shù)組元素連接成字符串
$password .= $chars[$keys[$i]];
}
return $password;
}
第三種取當(dāng)時(shí)時(shí)間戳
function get_password( $length = 8 )
{
$str = substr(md5(time()), 0, $length);//md5加密,time()當(dāng)前時(shí)間戳
return $str;
}
第四種打亂字符串
function getrandstr(){
$str='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
$randStr = str_shuffle($str);//打亂字符串
$rands= substr($randStr,0,6);//substr(string,start,length);返回字符串的一部分
return $rands;
}
第五種開始創(chuàng)建驗(yàn)證碼(直接用函數(shù)生成,比較方便快捷)
$code = rand(10000, 99999);
php mt_rand生成0~1隨機(jī)小數(shù)的效果比較
lcg_value說明
float lcg_value ( void )
lcg_value() 返回范圍為 (0, 1) 的一個(gè)偽隨機(jī)數(shù)。本函數(shù)組合了周期為 2^31 - 85 和 2^31 - 249 的兩個(gè)同余發(fā)生器。本函數(shù)的周期等于這兩個(gè)素?cái)?shù)的乘積。
返回:范圍為 (0, 1) 的偽隨機(jī)數(shù)。
<?php
for($i=0; $i<5; $i++){
echo lcg_value().PHP_EOL;
}
?>
輸出:
0.11516515851995
0.064684551575297
0.68275174031189
0.55730746529099
0.70215008878091
兩種生成0~1隨機(jī)小數(shù)方法進(jìn)行比較
1.執(zhí)行時(shí)間比較
執(zhí)行10萬次基于mt_rand()與mt_getrandmax()算法的運(yùn)行時(shí)間
<?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;
}
// 記錄開始時(shí)間
$starttime = get_microtime();
// 執(zhí)行10萬次獲取隨機(jī)小數(shù)
for($i=0; $i<100000; $i++){
randFloat();
}
// 記錄結(jié)束時(shí)間
$endtime = get_microtime();
// 輸出運(yùn)行時(shí)間
printf("run time %f ms\r\n", ($endtime-$starttime)*1000);
?>
輸出:
run time 266.893148 ms
執(zhí)行10萬次lcg_value()的運(yùn)行時(shí)間
<?php
// 獲取microtime
function get_microtime(){
list($usec, $sec) = explode(' ', microtime());
return (float)$usec + (float)$sec;
}
// 記錄開始時(shí)間
$starttime = get_microtime();
// 執(zhí)行10萬次獲取隨機(jī)小數(shù)
for($i=0; $i<100000; $i++){
lcg_value();
}
// 記錄結(jié)束時(shí)間
$endtime = get_microtime();
// 輸出運(yùn)行時(shí)間
printf("run time %f ms\r\n", ($endtime-$starttime)*1000);
?>
輸出:
run time 86.178064 ms
執(zhí)行時(shí)間上比較,因?yàn)?code>lcg_value()直接是php原生方法,而mt_rand()與mt_getrandmax()需要調(diào)用兩個(gè)方法,并需要進(jìn)行計(jì)算,因此lcg_value()的執(zhí)行時(shí)間大約快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; $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()的隨機(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; $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);
?>
3.隨機(jī)閱讀推薦
+ php給源碼加密的方法總結(jié):http://www.dbjr.com.cn/article/134506.html
以上就是如何在PHP中生成隨機(jī)數(shù)的詳細(xì)內(nèi)容,更多關(guān)于PHP隨機(jī)數(shù)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
生成隨機(jī)字符串和驗(yàn)證碼的類的PHP實(shí)例
這篇文章主要介紹了生成隨機(jī)字符串和驗(yàn)證碼的類的PHP實(shí)例,有需要的朋友可以參考一下2013-12-12
優(yōu)化WordPress的Google字體以加速國內(nèi)服務(wù)器上的運(yùn)行
這篇文章主要介紹了優(yōu)化WordPress的Google字體以加速國內(nèi)服務(wù)器上的運(yùn)行的方法,由于谷歌在內(nèi)地被墻導(dǎo)致其更新等方面都收到不同程度的影響,需要的朋友可以參考下2015-11-11
PHP實(shí)現(xiàn)搜索地理位置及計(jì)算兩點(diǎn)地理位置間距離的實(shí)例
這篇文章主要介紹了PHP實(shí)現(xiàn)搜索地理位置及計(jì)算兩點(diǎn)地理位置間距離的實(shí)例,地理位置搜尋的例子中使用到了MongoDB數(shù)據(jù)庫,需要的朋友可以參考下2016-01-01
安裝docker和docker-compose實(shí)例詳解
在本篇文章里小編給大家分享的是關(guān)于安裝docker和docker-compose的具體實(shí)例和代碼,需要的朋友們可以學(xué)習(xí)下。2019-07-07
基于jQueryUI和Corethink實(shí)現(xiàn)百度的搜索提示功能
這篇文章主要介紹了基于jQueryUI和Corethink實(shí)現(xiàn)百度的搜索提示功能,這里是以corethink模塊的形式,只需要安裝上訪問index.php?s=/test/index 就可以了,需要的朋友可以參考下2016-11-11
ThinkPHP框架基于PDO方式連接數(shù)據(jù)庫操作示例
這篇文章主要介紹了ThinkPHP框架基于PDO方式連接數(shù)據(jù)庫操作,結(jié)合完整實(shí)例形式分析了thinkPHP使用PDO方式連接數(shù)據(jù)庫的相關(guān)配置、控制器及模板調(diào)用相關(guān)操作技巧,需要的朋友可以參考下2018-03-03

