php - PHP数组随机/随机数组,所以没有两个相等的值不接近

原文 标签 php

所以我有一个数组[ A,A,B,B,B,C,C,C,C,C ],我需要以某种方式进行随机洗牌/随机化,它会随机排序,但尽可能保持相等的值。
最好的结果是[ C,A,B,C,A,B,C,B,C,C ]
我的代码:

<?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个连续C,最好是如果至少3个。C可以走到阵列前面

最佳答案

所以,我对代码做了一些调整

<?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
)
--

------

关于php - PHP数组随机/随机数组,所以没有两个相等的值不接近,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36350645/

相关文章:

javascript - XDK 重定向到模板文件将无法正常工作

php - 如何在Laravel 4路由组上应用多个过滤器?

php - PHP/Javascript制作浏览器游戏?

php - 扩展 COM 类?

php - 在同一页面上发布表单无法在php中工作

php - 用户等级算法

php - 为什么json_encode返回null?

php - MySQL 创建一个复杂的查询

php - 是否有用于生成迁移 SQL (postgres) 文件的脚本?

php - MPDF div在表格内不起作用