diff --git a/packages/support/src/Arr/functions.php b/packages/support/src/Arr/functions.php index 76f74d6fd..da66bcbc4 100644 --- a/packages/support/src/Arr/functions.php +++ b/packages/support/src/Arr/functions.php @@ -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 diff --git a/packages/support/tests/Arr/ManipulatesArrayTest.php b/packages/support/tests/Arr/ManipulatesArrayTest.php index 0a09a785a..197da7f62 100644 --- a/packages/support/tests/Arr/ManipulatesArrayTest.php +++ b/packages/support/tests/Arr/ManipulatesArrayTest.php @@ -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([