php生成多個(gè)不重復(fù)的隨機(jī)數(shù)實(shí)例程序
來(lái)源:程序員人生 發(fā)布時(shí)間:2014-05-16 08:01:08 閱讀次數(shù):3451次
在php中生成隨機(jī)數(shù)據(jù)直接使用mt_rand就可以實(shí)現(xiàn)了,如果要生成不重復(fù)隨機(jī)數(shù)我們可以使用 unique_rand 函數(shù)了,實(shí)例代碼如下:
- <?php
-
- $numbers = range (1,100);
-
- shuffle ($numbers);
-
- $no=6;
- $result = array_slice($numbers,0,$no);
- for ($i=0;$i<$no;$i++){
- echo $result[$i]."<br>";
- }
- print_r($result);
- ?>
-
- $numbers = range (1,42);
-
- shuffle ($numbers);
-
- $result = array_slice($numbers,0,3);
- print_r($result);
實(shí)例代碼二如下:
- <?php
- $numbers = range (1,20);
- srand ((float)microtime()*1000000);
- shuffle ($numbers);
- while (list (, $number) = each ($numbers)) {
- echo "$number ";
- }
- ?>
實(shí)例代碼三,用PHP,在1-20間隨機(jī)產(chǎn)生5個(gè)不重復(fù)的值,代碼如下:
- <?php
- function NoRand($begin=0,$end=20,$limit=5){
- $rand_array=range($begin,$end);
- shuffle($rand_array);
- return array_slice($rand_array,0,$limit);
- }
- print_r(NoRand());
- ?>
或者不shuffle的話,代碼如下:
- <?php
- $tmp=array();
- while(count($tmp)<5){
- $tmp[]=mt_rand(1,20);
- $tmp=array_unique($tmp);
- }
- print join(',',$tmp);
- ?>
上面都是紙上談兵了,下面來(lái)真實(shí)的了,要求如下:
有25幅作品拿去投票,一次投票需要選16幅,單個(gè)作品一次投票只能選擇一次,前面有個(gè)程序員捅了漏子,忘了把投票入庫(kù),有200個(gè)用戶產(chǎn)生的投票序列為空,那么你會(huì)如何填補(bǔ)這個(gè)漏子?
當(dāng)然向上級(jí)反映情況,但是我們這里討論的是技術(shù),就是需要生成1-25之間的16個(gè)不重復(fù)的隨機(jī)數(shù),去填補(bǔ),具體怎么設(shè)計(jì)函數(shù)呢?將隨機(jī)數(shù)存入數(shù)組,再在數(shù)組中去除重復(fù)的值,即可生成一定數(shù)量的不重復(fù)隨機(jī)數(shù),代碼如下:
- <?php
-
-
-
-
-
-
- function unique_rand($min, $max, $num) {
- $count = 0;
- $return = array();
- while ($count < $num) {
- $return[] = mt_rand($min, $max);
- $return = array_flip(array_flip($return));
- $count = count($return);
- }
- shuffle($return);
- return $return;
- }
- $arr = unique_rand(1, 25, 16);
- sort($arr);
- $result = '';
- for($i=0; $i < count($arr);$i++)
- {
- $result .= $arr[$i].',';
- }
- $result = substr($result, 0, -1);
- echo $result;
- ?>
程序運(yùn)行如下:1 2,3,4,6,7,8,9,10,11,12,13,16,20,21,22,24
補(bǔ)充幾點(diǎn)說(shuō)明:
•生成隨機(jī)數(shù)時(shí)用了 mt_rand() 函數(shù)。這個(gè)函數(shù)生成隨機(jī)數(shù)的平均速度要比 rand() 快四倍。
•去除數(shù)組中的重復(fù)值時(shí)用了“翻翻法”,就是用 array_flip() 把數(shù)組的 key 和 value 交換兩次。這種做法比用 array_unique() 快得多。
•返回?cái)?shù)組前,先使用 shuffle() 為數(shù)組賦予新的鍵名,保證鍵名是 0-n 連續(xù)的數(shù)字。如果不進(jìn)行此步驟,可能在刪除重復(fù)值時(shí)造成鍵名不連續(xù),給遍歷帶來(lái)麻煩。
再看一個(gè)實(shí)例:生成0-z這36個(gè)字符中的一個(gè)。每次調(diào)用 getOptions() 方法生成一個(gè)字符,它們的存儲(chǔ)如下:array[0] = 0, array[1] = 1, ……, array[35] = z,代碼如下:
- Array (
- [0] => 0
- [1] => 1
- [2] => 2
- [3] => 3
- [4] => 4
- [5] => 5
- [6] => 6
- [7] => 7
- [8] => 8
- [9] => 9
- [10] => a
- [11] => b
- [12] => c
- [13] => d
- [14] => e
- [15] => f
- [16] => g
- [17] => h
- [18] => i
- [19] => j
- [20] => k
- [21] => l
- [22] => m
- [23] => n
- [24] => o
- [25] => p
- [26] => q
- [27] => r
- [28] => s
- [29] => t
- [30] => u
- [31] => v
- [32] => w
- [33] => x
- [34] => y
- [35] => z
- )
然后在0-35之間隨機(jī)生成一個(gè)數(shù)作為索引,其實(shí)就是在上面數(shù)組中隨機(jī)取出一個(gè)數(shù),作為變量 $result 中的第一個(gè)字符,這個(gè)隨機(jī)索引隨后會(huì)被賦值成數(shù)組最后一個(gè),它將不會(huì)參與下一輪的隨機(jī)選取,代碼如下:
- <?php
-
- function getOptions()
- {
- $options = array();
- $result = array();
- for($i=48; $i<=57; $i++)
- {
- array_push($options,chr($i));
- }
- for($i=65; $i<=90; $i++)
- {
- $j = 32;
- $small = $i + $j;
- array_push($options,chr($small));
- }
- return $options;
- }
-
-
-
-
-
-
-
- $len = 10;
-
- for($j=0; $j<100; $j++)
- {
- $result = "";
- $options = getOptions();
- $lastIndex = 35;
- while (strlen($result)<$len)
- {
-
- $index = rand(0,$lastIndex);
-
- $chr = $options[$index];
-
- $result .= $chr;
- $lastIndex = $lastIndex-1;
-
- $options[$index] = $options[$lastIndex];
- }
- echo $result."n";
- }
- ?>
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)