Skip to content

Commit

Permalink
revert random change
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Feb 22, 2023
1 parent 6607201 commit 23b6cb7
Showing 1 changed file with 16 additions and 31 deletions.
47 changes: 16 additions & 31 deletions Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,32 +635,31 @@ public static function random($array, $number = null, $preserveKeys = false)
}

if (is_null($number)) {
return head(array_slice($array, random_int(0, $count - 1), 1));
return $array[array_rand($array)];
}

if ((int) $number === 0) {
return [];
}

$keys = array_keys($array);
$count = count($keys);
$selected = [];
$keys = array_rand($array, $number);

for ($i = $count - 1; $i >= $count - $number; $i--) {
$j = random_int(0, $i);
$results = [];

if ($preserveKeys) {
$selected[$keys[$j]] = $array[$keys[$j]];
} else {
$selected[] = $array[$keys[$j]];
if ($preserveKeys) {
foreach ((array) $keys as $key) {
$results[$key] = $array[$key];
}
} else {
foreach ((array) $keys as $key) {
$results[] = $array[$key];
}

$keys[$j] = $keys[$i];
}

return $selected;
return $results;
}


/**
* Set an array item to a given value using "dot" notation.
*
Expand Down Expand Up @@ -710,29 +709,15 @@ public static function set(&$array, $key, $value)
*/
public static function shuffle($array, $seed = null)
{
if (! is_null($seed)) {
if (is_null($seed)) {
shuffle($array);
} else {
mt_srand($seed);
shuffle($array);
mt_srand();

return $array;
}

if (empty($array)) {
return [];
}

$keys = array_keys($array);

for ($i = count($keys) - 1; $i > 0; $i--) {
$j = random_int(0, $i);
$shuffled[] = $array[$keys[$j]];
$keys[$j] = $keys[$i];
}

$shuffled[] = $array[$keys[0]];

return $shuffled;
return $array;
}

/**
Expand Down

0 comments on commit 23b6cb7

Please sign in to comment.