例子 1. mt_rand() 範例
| 代碼如下 |
複製代碼 |
<?php echo mt_rand() . "n"; echo mt_rand() . "n"; echo mt_rand(5, 15); ?> 上例的輸出類似於: 1604716014 1478613278 6 |
注: 自 PHP 4.2.0 起,不再需要用 srand() 或 mt_srand() 函數給隨機數發生器播種,現已自動完成。
注: 在 3.0.7 之前的版本中,max 的含義是 range。要在這些版本中得到和上例相同 5 到 15 的隨機數,簡短的例子是 mt_rand (5, 11)。
參見 mt_srand(),mt_getrandmax() 和 rand()。
rand() 函數返回隨機整數。
文法
rand(min,max)參數 描述
min,max 可選。規定隨機數產生的範圍。
說明
如果沒有提供選擇性參數 min 和 max,rand() 返回 0 到 RAND_MAX 之間的偽隨機整數。例如,想要 5 到 15(包括 5 和 15)之間的隨機數,用 rand(5, 15)。
提示和注釋
注釋:在某些平台下(例如 Windows)RAND_MAX 只有 32768。如果需要的範圍大於 32768,那麼指定 min 和 max 參數就可以產生大於 RAND_MAX 的數了,或者考慮用 mt_rand() 來替代它。
注釋:自 PHP 4.2.0 起,不再需要用 srand() 或 mt_srand() 函數給隨機數發生器播種,現在已自動完成。
注釋:在 3.0.7 之前的版本中,max 的含義是 range 。要在這些版本中得到和上例相同 5 到 15 的隨機數,簡短的例子是 rand (5, 11)。
mt_rand()真的會比rand()快4倍嗎?
帶著這個疑問,一邊自己測試一邊看網上的介紹,測試如下。
mt_rand()和rand()對比測試一
測試代碼:
| 代碼如下 |
複製代碼 |
<?php $max = 100000; $timeparts = explode(' ',microtime()); $stime = $timeparts[1].substr($timeparts[0],1); $i = 0; while($i < $max) { rand(); $i++; } $timeparts = explode(' ',microtime()); $etime = $timeparts[1].substr($timeparts[0],1); $time = $etime-$stime; echo "{$max} random numbers generated in {$time} seconds using rand(); "; $timeparts = explode(' ',microtime()); $stime = $timeparts[1].substr($timeparts[0],1); $i = 0; while($i < $max) { mt_rand(); $i++; } $timeparts = explode(' ',microtime()); $etime = $timeparts[1].substr($timeparts[0],1); $time = $etime-$stime; echo "{$max} random numbers generated in {$time} seconds using mt_rand(); "; ?> |
結果:
第一次測試
100000 random numbers generated in 0.024894952774048 seconds using rand();
100000 random numbers generated in 0.028925895690918 seconds using mt_rand();
第二次測試
100000 random numbers generated in 0.03147292137146 seconds using rand();
100000 random numbers generated in 0.02997088432312 seconds using mt_rand();
第三次測試
100000 random numbers generated in 0.028102874755859 seconds using rand();
100000 random numbers generated in 0.02803111076355 seconds using mt_rand();
第四次測試
100000 random numbers generated in 0.025573015213013 seconds using rand();
100000 random numbers generated in 0.028030157089233 seconds using mt_rand();
這個結果只是幾次的顯示結果,多測試幾次你會發覺,兩者是交替變化的,其實兩者沒有太大的差異。
mt_rand()和rand()對比測試二
本人測試環境
作業系統:windows xp
apache 2.0
php 5.2.12
記憶體 2G
| 代碼如下 |
複製代碼 |
<?php function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } $time_start = microtime_float(); for($i=0; $i<1000000; ++$i) { rand(); } $time_end = microtime_float(); $time = $time_end - $time_start; echo "rand() cost $time secondsn"; $time_start = microtime_float(); for($i=0; $i<1000000; ++$i) { mt_rand(); } $time_end = microtime_float(); $time = $time_end - $time_start; echo "mt_rand() cost $time secondsn"; ?>
|
結果:
第一次
rand() cost 0.25919604301453 seconds
mt_rand() cost 0.28554391860962 seconds
第二次
rand() cost 0.31136202812195 seconds
mt_rand() cost 0.28973197937012 seconds
第三次
rand() cost 0.27545690536499 seconds
mt_rand() cost 0.27108001708984 seconds
第四次
rand() cost 0.26263308525085 seconds
mt_rand() cost 0.27727103233337 seconds
結果還是一樣:兩者用的時間是交替變化,其實兩者沒有太大的差異
php的mt_rand()與rand()對比結論
在網上看了很多別人的測試,有linux的還有windows環境的,大多數人得出的結果和我的一樣:兩者相差無幾,不過也有人測出mt_rand()比rand()快4倍,但是由於他們沒給出具體的測試環境,所以無法判斷真假。我還是比較相信我的結論,因為我看到有人這樣介紹mt_rand()與rand():
那為什麼php手冊上說mt_rand()比rand()快4倍呢?
這是因為mt_rand()使用的Mersenne Twister algorythm是1997的事,所以在10年前,和rand()在速度上的差異是(4倍)。
自2004年,rand()已經開始使用algorythm,所以現在它們速度上沒有太大的區別
從上面的各種測試來看它們之間並沒有區別,只是在不同系統中可能數值會有變化了。