找到你要的答案

Q:shuffle / randomise array based on argument [duplicate]

Q:洗牌/基于辩论的[复制]随机阵列

This question already has an answer here:

I am searching for some kind of function or technique how to randomize/shuffle array based on parameter. I will try to describe what I am looking for.

Let's say we have:

$begin_array = array( 1,2,3,4,5,6,7,8,9,0 );

Then we call function:

magic_function( $begin_array, 1 );

And it returns the same randomized array every time based on second int argument of the function (1). If we provide 2, the returned array should be different from the first one but always in the same randomized order.

In other words this "magic" function should return randomized array based on the second integer argument.

The length of $begin_array is always different.

And the second argument can be up to 256.

Sample of what I am expecting:

<?php

$begin_array = array( 1,2,3,4,5,6,7,8,9,0 );

magic_function( $begin_array, 1 );
// always returns the same randomized array based on second int argument 1
// for ex: array( 5,2,1,8,3,9,6,4,7,0 );

magic_function( $begin_array, 2 );
// always returns the same randomized array based on second int argument 2
// for ex: array( 0,3,4,8,9,1,2,6,7,5 );

?>

I hope you understood me ;)

Thank you in advance, friends!

这个问题在这里已经有了答案:

I am searching for some kind of function or technique how to randomize/shuffle array based on parameter. I will try to describe what I am looking for.

让我们说我们有:

$begin_array = array( 1,2,3,4,5,6,7,8,9,0 );

然后我们调用函数:

magic_function( $begin_array, 1 );

And it returns the same randomized array every time based on second int argument of the function (1). If we provide 2, the returned array should be different from the first one but always in the same randomized order.

换句话说,这个“魔法”函数应该根据第二个整数参数返回随机数组。

begin_array美元的长度是不同的。

第二个参数可以高达256。

我期待的样本:

<?php

$begin_array = array( 1,2,3,4,5,6,7,8,9,0 );

magic_function( $begin_array, 1 );
// always returns the same randomized array based on second int argument 1
// for ex: array( 5,2,1,8,3,9,6,4,7,0 );

magic_function( $begin_array, 2 );
// always returns the same randomized array based on second int argument 2
// for ex: array( 0,3,4,8,9,1,2,6,7,5 );

?>

我希望你能理解我;

提前谢谢朋友们!

answer1: 回答1:

What you're looking for is a shuffle function that uses a random function that is seeded by the second argument.

You can do this by translating this Javascript array shuffle answer into PHP, and seeding the random number generator with the second argument:

function deterministic_shuffle(array $array, $seed)
{
    mt_srand($seed); // set the initial state
    // shuffle implementation using mt_rand()
    mt_srand(); // seed it again with random value to avoid affecting other code
    return $array;
}

你正在寻找的是一个洗牌函数,它使用一个随机函数,由第二个参数。

你可以通过将此JavaScript数组洗牌答案为PHP,和第二个参数随机数生成器的种子:

function deterministic_shuffle(array $array, $seed)
{
    mt_srand($seed); // set the initial state
    // shuffle implementation using mt_rand()
    mt_srand(); // seed it again with random value to avoid affecting other code
    return $array;
}
php  arrays