PHP的shuffle功能有多随机?

时间:2011-04-17 15:17:28

标签: php random shuffle

有谁知道PHP shuffle()函数的随机性是什么?它取决于操作系统吗? 它是否使用PHP自己的播种机?

是否可以使用mt_rand()作为生成器?

7 个答案:

答案 0 :(得分:14)

shuffle()函数基于与rand()相同的生成器,它是基于linear congruential algorithm的系统生成器。这是一个快速生成器,但随机性或多或少。从PHP 4.2.0开始,随机生成器会自动播种,但如果需要,可以使用srand()函数对其进行播种。

mtrand()基于Mersenne Twister algorithm,这是可用的最佳伪随机算法之一。要使用该生成器对数组进行洗牌,您需要编写自己的shuffle函数。您可以在Fisher-Yates algorithm查找示例。编写自己的shuffle函数会产生更好的随机性,但会比内置的shuffle函数慢。

答案 1 :(得分:5)

基于Mirouf的回答(非常感谢你的贡献)......我对它进行了一些改进以取出冗余的数组计数。我也根据自己的理解对变量进行了一些不同的命名。

如果你想像shuffle()那样使用它,你可以修改参数传递的参数,即& $ array,然后确保你将返回更改为:" return;&# 34;并将结果随机数组分配回$ array: $ array = $ randArr; (返回前)。

function mt_shuffle($array) {
    $randArr = [];
    $arrLength = count($array);

    // while my array is not empty I select a random position
    while (count($array)) {
        //mt_rand returns a random number between two values
        $randPos = mt_rand(0, --$arrLength);
        $randArr[] = $array[$randPos];

        /* If number of remaining elements in the array is the same as the
         * random position, take out the item in that position,
         * else use the negative offset.
         * This will prevent array_splice removing the last item.
         */
        array_splice($array, $randPos, ($randPos == $arrLength ? 1 : $randPos - $arrLength));
    }

    return $randArr;
}

答案 2 :(得分:4)

它是随机的,就像rand();

作为PHP风格,您不需要播种

答案 3 :(得分:4)

PHP 7.1的更新

由于rng_fixes rfc是针对PHP 7.1实现的,shuffle的实现现在使用了Mersenne Twister PRNG(即它使用mt_rand并且受到调用mt_srand的影响

遗留系统PRNG(rand)不再可用;函数randsrand实际上是其mt_等效的别名。

答案 4 :(得分:2)

mt_rand()

生成随机数。

shuffle()

随机化一个数组。它还会在数组中生成新键,而不是仅重新排列旧键。

如果你想在PHP中播种,你会使用mt_strand()

但是,由于PHP 4.2.0播种是在调用mt_rand时自动在PHP中完成的。

答案 5 :(得分:1)

使用关联数组和数字数组:

function mt_shuffle_array($array) {
    $shuffled_array = [];
    $arr_length = count($array);

    if($arr_length < 2) {
        return $array;
    }

    while($arr_length) {
        --$arr_length;
        $rand_key = array_keys($array)[mt_rand(0, $arr_length)];

        $shuffled_array[$rand_key] = $array[$rand_key];
        unset($array[$rand_key]);
    }

    return $shuffled_array;
}

$array = [-2, -1, 'a' => '1', 'b' => '2', 'c' => '3', 11, 'd' => '4', 22];
$shuffled_array = mt_shuffle_array($array);

enter image description here

答案 6 :(得分:0)

我创建了一个随机排序数组的函数。

/**
 * Build a random array
 *
 * @param mixed $array
 *
 * @return array
 */
function random_array($array) {
    $random_array = array();
    // array start by index 0
    $countArray = count($array) - 1;

    // while my array is not empty I build a random value
    while (count($array) != 0) {
        //mt_rand return a random number between two value 
        $randomValue = mt_rand(0, $countArray);
        $random_array[] = $array[$randomValue];

        // If my count of my tab is 4 and mt_rand give me the last element, 
        // array_splice will not unset the last item
        if(($randomValue + 1) == count($array)) {
            array_splice($array, $randomValue, ($randomValue - $countArray + 1));
        } else {
            array_splice($array, $randomValue, ($randomValue - $countArray));
        }

        $countArray--;
    }

    return $random_array;
}

这不是最好的方法,但是当我使用函数shuffle时,它总是以相同的顺序返回一个随机数组。如果这可以帮助某人,我会很高兴!