Skip to content

Instantly share code, notes, and snippets.

@fetus-hina
Created September 18, 2012 11:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fetus-hina/3742682 to your computer and use it in GitHub Desktop.
Save fetus-hina/3742682 to your computer and use it in GitHub Desktop.
mt_shuffle(), array_shuffle()
<?php
function fy_shuffle(array &$array, $rand = 'mt_rand') {
$array = array_values($array);
for($i = count($array) - 1; $i > 0; --$i) {
$j = $rand(0, $i);
if($i !== $j) {
list($array[$i], $array[$j]) = array($array[$j], $array[$i]);
}
}
return true;
}
function mt_shuffle(array &$array) {
return fy_shuffle($array, 'mt_rand');
}
function array_shuffle(array $array, $rand = 'mt_rand') {
fy_shuffle($array, $rand);
return $array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment