Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Randomize a PHP array with a seed?

I'm looking for a function that I can pass an array and a seed to in PHP and get back a "randomized" array. If I passed the same array and same seed again, I would get the same output.

I've tried this code

 //sample array $test = array(1,2,3,4,5,6); //show the array print_r($test);  //seed the random number generator mt_srand('123'); //generate a random number based on that echo mt_rand(); echo "\n";  //shuffle the array shuffle($test);  //show the results print_r($test);  

But it does not seem to work. Any thoughts on the best way to do this?

This question dances around the issue but it's old and nobody has provided an actual answer on how to do it: Can i randomize an array by providing a seed and get the same order? - "Yes" - but how?

Update

The answers so far work with PHP 5.1 and 5.3, but not 5.2. Just so happens the machine I want to run this on is using 5.2.

Can anyone give an example without using mt_rand? It is "broken" in php 5.2 because it will not give the same sequence of random numbers based off the same seed. See the php mt_rand page and the bug tracker to learn about this issue.

like image 285
cwd Avatar asked Jul 02 '11 15:07

cwd


People also ask

How do I randomize an array in PHP?

The shuffle() Function is a builtin function in PHP and is used to shuffle or randomize the order of the elements in an array. This function assigns new keys for the elements in the array. It will also remove any existing keys, rather than just reordering the keys and assigns numeric keys starting from zero.

How are random numbers generated from a seed?

Random seeds are often generated from the state of the computer system (such as the time), a cryptographically secure pseudorandom number generator or from a hardware random number generator.

What is seed in randomization?

The seed() method is used to initialize the random number generator. The random number generator needs a number to start with (a seed value), to be able to generate a random number. By default the random number generator uses the current system time.

How do I shuffle a variable in PHP?

PHP shuffle() Function $my_array = array("red","green","blue","yellow","purple"); shuffle($my_array); print_r($my_array);


2 Answers

You can use array_multisort to order the array values by a second array of mt_rand values:

$arr = array(1,2,3,4,5,6);  mt_srand('123'); $order = array_map(create_function('$val', 'return mt_rand();'), range(1, count($arr))); array_multisort($order, $arr);  var_dump($arr); 

Here $order is an array of mt_rand values of the same length as $arr. array_multisort sorts the values of $order and orders the elements of $arr according to the order of the values of $order.

like image 40
Gumbo Avatar answered Oct 06 '22 00:10

Gumbo


Sorry, but accordingly to the documentation the shuffle function is seeded automatically.

Normally, you shouldn't try to come up with your own algorithms to randomize things since they are very likely to be biased. The Fisher-Yates algorithm is known to be both efficient and unbiased though:

function fisherYatesShuffle(&$items, $seed) {     @mt_srand($seed);     for ($i = count($items) - 1; $i > 0; $i--)     {         $j = @mt_rand(0, $i);         $tmp = $items[$i];         $items[$i] = $items[$j];         $items[$j] = $tmp;     } } 

Example (PHP 5.5.9):

php > $original = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); php > $shuffled = (array)$original; php > fisherYatesShuffle($shuffled, 0); php > print_r($shuffled); Array (     [0] => 6     [1] => 0     [2] => 7     [3] => 2     [4] => 9     [5] => 3     [6] => 1     [7] => 8     [8] => 5     [9] => 4 ) php > $shuffled = (array)$original; php > fisherYatesShuffle($shuffled, 0); php > print_r($shuffled); Array (     [0] => 6     [1] => 0     [2] => 7     [3] => 2     [4] => 9     [5] => 3     [6] => 1     [7] => 8     [8] => 5     [9] => 4 ) 
like image 81
André Laszlo Avatar answered Oct 05 '22 22:10

André Laszlo