Skip to content

Commit

Permalink
update FisherYatesShuffle
Browse files Browse the repository at this point in the history
  • Loading branch information
nepster-web committed Nov 11, 2021
1 parent efdc828 commit 2cd9555
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/FisherYatesShuffle.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ class FisherYatesShuffle
*/
public function __invoke(array $array): array
{
foreach ($array as $i => $iValue) {
$r = Random::getInteger(0, $i);
$tmp = $iValue;
$count = count($array);

for ($i = 0; $i < $count - 1; $i++) {
$r = Random::getInteger(0, $count - 1);
$tmp = $array[$i];
$array[$i] = $array[$r];
$array[$r] = $tmp;
}
Expand Down

0 comments on commit 2cd9555

Please sign in to comment.