2016-04-01 33 views
3

所以我有一個數組[ A,A,B,B,B,C,C,C,C,C ],我需要隨機混洗/隨機化,它會隨機地排序項目,但保持相同的值儘可能分開。 最好的結果將是[ C,A,B,C,A,B,C,B,C,C ]php array shuffle/randomize array所以沒有兩個相等的值不在

到目前爲止我的代碼:

<?php 

$UnitedList = [ 
    'A', 
    'A', 
    'B', 
    'B', 
    'B', 
    'C', 
    'C', 
    'C', 
    'C', 
    'C' 
]; 

$mixed_list = []; 
$i = 0; 

function getKey(&$array, $not) { 
    $rk = array_rand($array); 
    // echo $rk; 
    if ($array[$rk] !== $not || count(array_unique($array)) === 1) { 
     return $rk; 
    } 

    return getKey($array, $not); 
} 

while (!empty($UnitedList)) { 
    $randomk = array_rand($UnitedList); 

    if ($i === 0) { 
     $mixed_list[] = $UnitedList[$randomk]; 
     unset($UnitedList[$randomk]); 
     ++$i; 
     continue; 
    } 

    if (isset($mixed_list[$i - 1]) && $mixed_list[$i - 1] !== $UnitedList[$randomk]) { 
     $mixed_list[] = $UnitedList[$randomk]; 
     unset($UnitedList[$randomk]); 
     ++$i; 
     continue; 
    } 

    if (isset($mixed_list[$i - 1]) && $mixed_list[$i - 1] === $UnitedList[$randomk]) { 
     $newk = getKey($UnitedList, $UnitedList[$randomk]); 
     $mixed_list[] = $UnitedList[$newk]; 
     unset($UnitedList[$newk]); 
     ++$i; 
     continue; 
    } 

    ++$i; 
    continue; 
} 

print_r($mixed_list); 

>

最好成績:

Array 
(
    [0] => A 
    [1] => C 
    [2] => A 
    [3] => C 
    [4] => B 
    [5] => C 
    [6] => B 
    [7] => C 
    [8] => B 
    [9] => C 
) 

最壞的結果:

Array 
(
    [0] => A 
    [1] => B 
    [2] => A 
    [3] => C 
    [4] => B 
    [5] => C 
    [6] => B 
    [7] => C 
    [8] => C 
    [9] => C 
) 

我需要以某種方式,禁止最壞的結果類型的結果,當有一排3℃,更好的是,如果至少3 C可在陣列

+0

所以,你不想隨意。在這種情況下,我建議你創建一個理想的訂單列表,並且只是隨機化開始點。 – Burki

+0

不能這樣做,因爲INPUT列表是動態的,一次可以是[A,A,A,B,B,C,C,C,D,D]其他[A,A,B,B,C ] – Makarons

+0

將數組排序爲唯一的組,然後隨機選擇一個組。跟蹤該組並隨機選擇一個不屬於當前組的組。重複,直到值用完。 – Tigger

回答

0

所以前面走,我做了一些調整代碼

<?php 
    function getKey(&$array, $not) { 
     $rk = array_rand($array); 
     if ($array[$rk] !== $not || count(array_unique($array)) === 1) { 
      return $rk; 
     } 

     return getKey($array, $not); 
    } 

    function getRandomizedList($listToRandomize) { 
     $mixed_list = []; 
     $i = 0; 
     shuffle($listToRandomize); 

     while (!empty($listToRandomize)) { 
      $randomk = array_rand($listToRandomize); 

      if ($i === 0) { 
       $mixed_list[] = $listToRandomize[$randomk]; 
       unset($listToRandomize[$randomk]); 
       ++$i; 
       continue; 
      } 

      if (isset($mixed_list[$i - 1]) && $mixed_list[$i - 1] !== $listToRandomize[$randomk]) { 
       $mixed_list[] = $listToRandomize[$randomk]; 
       unset($listToRandomize[$randomk]); 
       ++$i; 
       continue; 
      } 

      if (isset($mixed_list[$i - 1]) && $mixed_list[$i - 1] === $listToRandomize[$randomk]) { 
       $newk = getKey($listToRandomize, $listToRandomize[$randomk]); 
       if ($mixed_list[$i - 1] === $listToRandomize[$newk] && $mixed_list[0] !== $listToRandomize[$newk]) { 
        array_unshift($mixed_list, $listToRandomize[$newk]); 
       } else { 
        $mixed_list[] = $listToRandomize[$newk]; 
       } 
       unset($listToRandomize[$newk]); 
       ++$i; 
       continue; 
      } 

      ++$i; 
      continue; 
     } 
     return $mixed_list; 
    } 


    $UnitedList1 = [ 
     'C', 
     'C', 
     'C', 
     'C', 
     'C', 
     'A', 
     'A', 
     'B', 
     'B', 
     'B' 
    ]; 

    $UnitedList2 = [ 
     'C', 
     'C', 
     'C', 
     'C', 
     'C', 
     'A', 
     'A', 
     'A', 
     'A', 
     'A', 
     'B', 
     'B', 
     'B' 
    ]; 

    for ($i=0; $i < 5; $i++) { 
     echo "UnitedList1 i = $i"; 
     print_r(getRandomizedList($UnitedList1)); 
     echo "--\n\r"; 
    } 
    echo "------\n\r"; 


    for ($i=0; $i < 5; $i++) { 
     echo "UnitedList2 i = $i"; 
     print_r(getRandomizedList($UnitedList2)); 
     echo "--\n\r"; 
    } 
    echo "------\n\r"; 
?> 

和結果:

UnitedList1 i = 0Array 
(
    [0] => C 
    [1] => B 
    [2] => C 
    [3] => A 
    [4] => B 
    [5] => C 
    [6] => A 
    [7] => C 
    [8] => B 
    [9] => C 
) 
-- 

UnitedList1 i = 1Array 
(
    [0] => C 
    [1] => A 
    [2] => B 
    [3] => C 
    [4] => A 
    [5] => C 
    [6] => B 
    [7] => C 
    [8] => B 
    [9] => C 
) 
-- 

UnitedList1 i = 2Array 
(
    [0] => C 
    [1] => B 
    [2] => A 
    [3] => C 
    [4] => B 
    [5] => C 
    [6] => A 
    [7] => B 
    [8] => C 
    [9] => C 
) 
-- 

UnitedList1 i = 3Array 
(
    [0] => C 
    [1] => A 
    [2] => B 
    [3] => C 
    [4] => A 
    [5] => B 
    [6] => C 
    [7] => B 
    [8] => C 
    [9] => C 
) 
-- 

UnitedList1 i = 4Array 
(
    [0] => C 
    [1] => B 
    [2] => C 
    [3] => B 
    [4] => A 
    [5] => C 
    [6] => B 
    [7] => C 
    [8] => A 
    [9] => C 
) 
-- 

------ 

UnitedList2 i = 0Array 
(
    [0] => C 
    [1] => A 
    [2] => C 
    [3] => A 
    [4] => C 
    [5] => A 
    [6] => B 
    [7] => C 
    [8] => B 
    [9] => C 
    [10] => A 
    [11] => B 
    [12] => A 
) 
-- 

UnitedList2 i = 1Array 
(
    [0] => C 
    [1] => A 
    [2] => C 
    [3] => A 
    [4] => C 
    [5] => A 
    [6] => B 
    [7] => C 
    [8] => A 
    [9] => C 
    [10] => B 
    [11] => A 
    [12] => B 
) 
-- 

UnitedList2 i = 2Array 
(
    [0] => A 
    [1] => B 
    [2] => C 
    [3] => A 
    [4] => C 
    [5] => A 
    [6] => C 
    [7] => B 
    [8] => A 
    [9] => C 
    [10] => B 
    [11] => C 
    [12] => A 
) 
-- 

UnitedList2 i = 3Array 
(
    [0] => C 
    [1] => A 
    [2] => C 
    [3] => B 
    [4] => A 
    [5] => C 
    [6] => B 
    [7] => A 
    [8] => B 
    [9] => A 
    [10] => C 
    [11] => A 
    [12] => C 
) 
-- 

UnitedList2 i = 4Array 
(
    [0] => A 
    [1] => C 
    [2] => A 
    [3] => B 
    [4] => C 
    [5] => B 
    [6] => A 
    [7] => C 
    [8] => A 
    [9] => B 
    [10] => C 
    [11] => A 
    [12] => C 
) 
-- 

------ 
0

第三次嘗試:

<?php 

    $groups = [ 
     [ 'A','A' ], 
     [ 'B','B','B' ], 
     [ 'C','C','C','C','C' ] 
    ]; 

    function generateRandomList($groups) { 
     $output = []; 
     $prev_k = null; 

     shuffle($groups); 
     do { 
      $active_group_k = array_rand($groups); 
      if ($prev_k === $active_group_k && count($groups) > 1) { 
       continue; 
      } 

      $output[] = array_pop($groups[$active_group_k]); 

      if (empty($groups[$active_group_k])) { 
       unset($groups[$active_group_k]); 
      } 
      $prev_k = $active_group_k; 

     } while (!empty($groups)); 

     return $output; 
    } 
    echo "randomize when in groups: \n"; 

    for ($i=0; $i < 5; $i++) { 
     echo json_encode(generateRandomList($groups)) . "\n"; 
    } 

    echo "--- \n"; 

    function getKey(&$array, $not) { 
     $rk = array_rand($array); 
     if ($array[$rk] !== $not || count(array_unique($array)) === 1) { 
      return $rk; 
     } 

     return getKey($array, $not); 
    } 

    function getRandomizedList($listToRandomize) { 
     $mixed_list = []; 
     $i = 0; 
     shuffle($listToRandomize); 

     while (!empty($listToRandomize)) { 
      $randomk = array_rand($listToRandomize); 

      if ($i === 0) { 
       $mixed_list[] = $listToRandomize[$randomk]; 
       unset($listToRandomize[$randomk]); 
       ++$i; 
       continue; 
      } 

      if (isset($mixed_list[$i - 1]) && $mixed_list[$i - 1] !== $listToRandomize[$randomk]) { 
       $mixed_list[] = $listToRandomize[$randomk]; 
       unset($listToRandomize[$randomk]); 
       ++$i; 
       continue; 
      } 

      if (isset($mixed_list[$i - 1]) && $mixed_list[$i - 1] === $listToRandomize[$randomk]) { 
       $newk = getKey($listToRandomize, $listToRandomize[$randomk]); 
       if ($mixed_list[$i - 1] === $listToRandomize[$newk] && $mixed_list[0] !== $listToRandomize[$newk]) { 
        array_unshift($mixed_list, $listToRandomize[$newk]); 
       } else { 
        $mixed_list[] = $listToRandomize[$newk]; 
       } 
       unset($listToRandomize[$newk]); 
       ++$i; 
       continue; 
      } 

      ++$i; 
      continue; 
     } 
     return $mixed_list; 
    } 


    $UnitedList1 = [ 
     'A', 
     'A', 
     'B', 
     'B', 
     'B', 
     'C', 
     'C', 
     'C', 
     'C', 
     'C' 
    ]; 

    echo "randomize when all in one array: \n"; 
    for ($i=0; $i < 5; $i++) { 
     echo json_encode(getRandomizedList($UnitedList1)) . "\n"; 
    } 

?> 

和結果:

randomize when in groups: 
["A","C","A","C","B","C","B","C","B","C"] 
["C","A","C","B","A","C","B","C","B","C"] 
["C","A","C","B","C","A","B","C","B","C"] 
["C","A","C","B","A","C","B","C","B","C"] 
["A","B","A","B","C","B","C","C","C","C"] 
--- 
randomize when all in one array: 
["C","A","C","B","C","B","A","C","B","C"] 
["C","B","C","A","C","B","C","A","B","C"] 
["C","B","A","C","A","C","B","C","B","C"] 
["C","B","A","B","C","A","C","B","C","C"] 
["C","A","C","A","C","B","C","B","C","B"] 

看來,那個分組沒有幫助!

0
  1. 嘗試 - 否我做了檢查,如果有任何值等於之前的值,如果是,然後再生。

代碼:

<?php 

    $groups = [ 
     [ 'A','A' ], 
     [ 'B','B','B' ], 
     [ 'C','C','C','C','C' ] 
    ]; 

    function generateRandomList($groups) { 
     $output = []; 
     $prev_k = null; 
     $orginal_groups = $groups; 

     shuffle($groups); 
     do { 
      $active_group_k = array_rand($groups); 
      if ($prev_k === $active_group_k && count($groups) > 1) { 
       continue; 
      } 

      $output[] = array_pop($groups[$active_group_k]); 

      if (empty($groups[$active_group_k])) { 
       unset($groups[$active_group_k]); 
      } 
      $prev_k = $active_group_k; 

     } while (!empty($groups)); 

     foreach ($output as $key => $value) { 
      if ($key > 0) { 
       if ($value === $output[$key - 1]) { 
        $output = generateRandomList($orginal_groups); 
       } 
      } 
     } 

     return $output; 
    } 
    echo "randomize when in groups: \n"; 

    for ($i=0; $i < 5; $i++) { 
     echo json_encode(generateRandomList($groups)) . "\n"; 
    } 

結果:

randomize when in groups: 
["A","C","A","C","B","C","B","C","B","C"] 
["C","B","A","C","A","C","B","C","B","C"] 
["A","C","A","C","B","C","B","C","B","C"] 
["C","A","C","A","C","B","C","B","C","B"] 
["C","A","C","A","C","B","C","B","C","B"]