phpOMS/Utils/Random/ArrayRandomize.php
Dennis Eichhorn 78bc28b045 Draft
All these files need further edits + other optimization files + moving
of files
2016-03-13 21:49:01 +01:00

30 lines
580 B
PHP

<?php
class ArrayRandomize
{
public static function yates($arr) : array
{
$shuffled = [];
while($arr){
$rnd = array_rand($arr);
$shuffled[] = $arr[$rnd];
array_splice($arr, $rnd, 1);
}
return $shuffled;
}
public static function knuth($arr) : array
{
$shuffled = [];
for($i = count($arr)-1; $i > 0; $i--){
$rnd = mt_rand(0, $i);
$shuffled[$i] = $arr[$rnd];
$shuffled[$rand] = $arr[$i];
}
return $shuffled;
}
}