-
-
Save achmadfatoni/25188f3eb7ca2165085747c4bf8c44ec to your computer and use it in GitHub Desktop.
This file contains 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 | |
/** | |
* 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