Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/support/src/Arr/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ function random(iterable $array, int $number = 1, bool $preserveKey = false): mi
}

if ($preserveKey === false) {
shuffle($randomValues);
$randomValues = shuffle($randomValues);
}

return count($randomValues) > 1
Expand Down
9 changes: 9 additions & 0 deletions packages/support/tests/Arr/ManipulatesArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,15 @@ public function test_random(): void
$this->assertCount(3, $randoms);
}

public function test_random_without_preserving_keys_shuffles_selected_values(): void
{
$randoms = arr(range(1, 1_000))->random(100)->toArray();
$ordered = $randoms;
sort($ordered);

$this->assertNotEquals($randoms, $ordered);
}

public function test_random_with_preserve_keys(): void
{
$collection = arr([
Expand Down
Loading