Skip to content

Instantly share code, notes, and snippets.

@achmadfatoni
Forked from thinkphp/fisher-yates-shuffle.php
Created November 10, 2018 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save achmadfatoni/25188f3eb7ca2165085747c4bf8c44ec to your computer and use it in GitHub Desktop.
Save achmadfatoni/25188f3eb7ca2165085747c4bf8c44ec to your computer and use it in GitHub Desktop.
<?php
/**
* It's an algorithm for generating a random permutation of a finit sequence - in plain terms, the algorithm shuffles the sequence.
*
* Reference: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
*/
function MyShuffle(&$arr) {
for($i = 0; $i < sizeof($arr); ++$i) {
$r = rand(0, $i);
$tmp = $arr[$i];
$arr[$i] = $arr[$r];
$arr[$r] = $tmp;
}
};
echo"<h2>Hello Fisher-Yates Shuffle Algorithm</h2>";
$arr = array(0,1,2,3,4,5,6,7,8,9);
echo"<pre>";
print_r($arr);
echo"</pre>";
MyShuffle($arr);
echo"<pre>";
print_r($arr);
echo"</pre>";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment