[英]Php Random Sequence Generator
我希望創建一個具有與我的條件相對應的隨機序列的數組。
這是一個很好的例子:1,4,5,7,| 8,6,3,2
這不是一個很好的例子:1,3,2,6,| 5,7,8,4,因為如果1,3,2是連續的數字(如果在前4位中排序(1,2,3)
我做的:
$sequences = array();
while(count($sequences) < 100){
//Random 8 numbers sequence from 1-8
$sequence = array();
while(count($sequence) < 8){
$rand = rand(1,8);
if(!in_array($rand, $sequence)){
array_push($sequence, $rand);
}
}
//Insert if numbers are not successive.
//Struggling here
if(?????){
array_push($sequences, $sequence);
}
}
print_r($sequences);
它適用於它的生成部分,但是我不知道如何插入不包含連續數字的序列。 有什么想法嗎?
您可以將兩組數字表示為1或0,並通過選擇帶有四個1和0且沒有3個連續1或0的序列的8位二進制字符串之一來選擇每個集合中的元素。 這些二進制字符串中的每一個均由以下整數之一表示:
43 45 51 53 54 75 77 83 85 86 89 90 101 102 105 106 108
147 149 150 153 154 165 166 169 170 172 178 180 201 202 204 210 212
下半部分與上半部分完全對稱,因此我們可以從上半部分中選取一個數字,然后進行一些處理以選擇整個集合:
$combinations = array(43,45,51,53,54,75,77,83,85,86,89,90,101,102,105,106,108);
// Pick a random combination
$combination = $combinations[array_rand($combinations)];
if (mt_rand() & 1)
$combination = 255 - $combination;
$combination = str_split(str_pad(decbin($combination), 8, '0', STR_PAD_LEFT));
// Get the first four values
$first = array_keys(array_filter($combination, function($x){ return $x == '0'; }));
shuffle($first); // Permute them
// Get the last four values
$last = array_keys(array_filter($combination, function($x){ return $x == '1'; }));
shuffle($last); // Permute them
$result = array_map(function($x){ return $x + 1; }, array_merge($first, $last));
這將是一個隨機序列,並且您的約束均勻地隨機分布,並且應該非常有效。 一些樣本輸出:
[6, 2, 3, 8, 1, 4, 5, 7]
[1, 6, 2, 4, 3, 5, 8, 7]
[6, 3, 1, 8, 2, 4, 5, 7]
[5, 8, 4, 2, 7, 1, 6, 3]
[5, 2, 8, 1, 3, 6, 7, 4]
[8, 6, 2, 4, 7, 1, 3, 5]
[7, 1, 5, 4, 8, 3, 2, 6]
[5, 1, 3, 6, 2, 7, 8, 4]
[1, 7, 2, 5, 3, 8, 4, 6]
[5, 6, 3, 8, 2, 7, 4, 1]
這可能是一種無效的方法(就資源而言),可以被更多地認為是一種hack。
$sequence = array(1,2,4,5,8,9,5,7);
usort($sequence, function($a, $b) {
//if $a + 1 = $b, then $b + 1;
return ($a + 1) == $b ? $b = $b + 1 : 1;
});
print_r($sequence);
這將返回;
Array ( [0] => 5 [1] => 8 [2] => 2 [3] => 1 [4] => 4 [5] => 9 [6] => 5 [7] => 7 )
它並不是一個漂亮的代碼,我可以縮短它,但是仍然可以正常工作。
$sequence = array();
$sequences = array();
$loops = 0;
while($loops < 10000){
$loops++;
//Get a sequence
while(count($sequence) < 8){
$rand = rand(1,8);
if(!in_array($rand, $sequence)){
array_push($sequence, $rand);
}
}
//Checks if sequence meet requirements
//This part could be summarized in a function
$first = array($sequence[0], $sequence[1], $sequence[2],$sequence[3]);
$last = array($sequence[4], $sequence[5], $sequence[6],$sequence[7]);
sort($first);
sort($last);
if($first[1] != $first[0]+1 || $first[1] !=$first[2]-1){
if($first[2] != $first[1]+1 || $first[2] !=$first[3]-1){
if($last[1] != $last[0]+1 || $last[1] !=$last[2]-1){
if($last[2] != $last[1]+1 || $last[2] !=$last[3]-1){
$sequence = array_merge($first, $last);
if(!in_array($sequence, $sequences)){
array_push($sequences, $sequence);
}
}
}
}
}
$sequence = array();
}
print_r($sequences);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.