php產生多個不重複的隨機數執行個體程式

來源:互聯網
上載者:User

 代碼如下:

<?php
//range 是將1到100 列成一個數組
$numbers = range (1,100);
//shuffle 將數組順序隨即打亂
shuffle ($numbers);
//array_slice 取該數組中的某一段
$no=6;
$result = array_slice($numbers,0,$no);
for ($i=0;$i<$no;$i++){
echo $result[$i]."<br>";
}
print_r($result);
?>


//range 是將1到42 列成一個數組
$numbers = range (1,42);
//shuffle 將數組順序隨即打亂
shuffle ($numbers);
//array_slice 取該數組中的某一段
$result = array_slice($numbers,0,3);
print_r($result);

 

方法2

代碼如下


<?php
$numbers = range (1,20);
srand ((float)microtime()*1000000);
shuffle ($numbers);
while (list (, $number) = each ($numbers)) {
echo "$number ";
}
?>

 

方法3

用PHP,在1-20間隨機產生5個不重複的值,如何做

代碼如下


<?php
function NoRand($begin=0,$end=20,$limit=5){
$rand_array=range($begin,$end);
shuffle($rand_array);//調用現成的數組隨機排列函數
return array_slice($rand_array,0,$limit);//截取前$limit個
}
print_r(NoRand());
?>

 

或者不shuffle的話

代碼如下


<?php
$tmp=array();
while(count($tmp)<5){
$tmp[]=mt_rand(1,20);
$tmp=array_unique($tmp);
}
print join(',',$tmp);
?>


上面都是紙上談兵了,下面來真實的了,要求如下

有25幅作品拿去投票,一次投票需要選16幅,單個作品一次投票只能選擇一次。前面有個程式員捅了漏子,忘了把投票入庫,有200個使用者產生的投票序列為空白。那麼你會如何填補這個漏子?

當然向上級反映情況。但是我們這裡討論的是技術,就是需要產生1-25之間的16個不重複的隨機數,去填補。具體怎麼設計函數呢?將隨機數存入數組,再在數組中去除重複的值,即可產生一定數量的不重複隨機數

代碼如下


<?php
/*
* array unique_rand( int $min, int $max, int $num )
* 產生一定數量的不重複隨機數
* $min 和 $max: 指定隨機數的範圍
* $num: 指定產生數量
*/
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;
?>

程式運行如下:

1 2,3,4,6,7,8,9,10,11,12,13,16,20,21,22,24

 

補充幾點說明:

•產生隨機數時用了 mt_rand() 函數。這個函數產生隨機數的平均速度要比 rand() 快四倍。
•去除數組中的重複值時用了“翻翻法”,就是用 array_flip() 把數組的 key 和 value 交換兩次。這種做法比用 array_unique() 快得多。

•返回數組前,先使用 shuffle() 為數組賦予新的鍵名,保證鍵名是 0-n 連續的數字。如果不進行此步驟,可能在重複資料刪除值時造成鍵名不連續,給遍曆帶來麻煩。


再看一執行個體

產生0-z這36個字元中的一個。每次調用 getOptions() 方法產生一個字元,它們的儲存如下: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之間隨機產生一個數作為索引,其實就是在上面數組中隨機取出一個數,作為變數 $result 中的第一個字元。這個隨機索引隨後會被賦值成數組最後一個,它將不會參與下一輪的隨機選取。

代碼如下


<?php
// 產生0123456789abcdefghijklmnopqrstuvwxyz中的一個字元
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;
}
/*
$e = getOptions();
for($j=0; $j<150; $j++)
{
echo $e[$j];
}
*/
$len = 10;
// 隨機產生數組索引,從而實現隨機數
for($j=0; $j<100; $j++)
{
$result = "";
$options = getOptions();
$lastIndex = 35;
while (strlen($result)<$len)
{
// 從0到35中隨機取一個作為索引
$index = rand(0,$lastIndex);
// 將隨機數賦給變數 $chr
$chr = $options[$index];
// 隨機數作為 $result 的一部分
$result .= $chr;
$lastIndex = $lastIndex-1;
// 最後一個索引將不會參與下一次隨機抽獎
$options[$index] = $options[$lastIndex];
}
echo $result."n";
}
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.