Created
January 21, 2017 19:31
-
-
Save fdcore/a906b2460ff85570d618f24afbf05bc5 to your computer and use it in GitHub Desktop.
Функция для псевдослучайного перемешивания массива, используя семя случайностей
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
@param array массив для перемешивания | |
@param int семя случайных чисел | |
*/ | |
function seededShuffle(array &$array, $seed) { | |
mt_srand($seed); | |
$size = count($array); | |
for ($i = 0; $i < $size; ++$i) { | |
list($chunk) = array_splice($array, mt_rand(0, $size-1), 1); | |
array_push($array, $chunk); | |
} | |
} | |
// пример | |
$array = range(1, 10); // массив для перемешивания | |
$seed = 1; // семя | |
seededShuffle($array, $seed); // мешаем | |
echo implode(',', $array); // выводим |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment