Skip to content

Commit cf3eb90

Browse files
committed
revert random change
1 parent 24e631f commit cf3eb90

File tree

2 files changed

+17
-69
lines changed

2 files changed

+17
-69
lines changed

src/Illuminate/Collections/Arr.php

+16-31
Original file line numberDiff line numberDiff line change
@@ -635,32 +635,31 @@ public static function random($array, $number = null, $preserveKeys = false)
635635
}
636636

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

641641
if ((int) $number === 0) {
642642
return [];
643643
}
644644

645-
$keys = array_keys($array);
646-
$count = count($keys);
647-
$selected = [];
645+
$keys = array_rand($array, $number);
648646

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

652-
if ($preserveKeys) {
653-
$selected[$keys[$j]] = $array[$keys[$j]];
654-
} else {
655-
$selected[] = $array[$keys[$j]];
649+
if ($preserveKeys) {
650+
foreach ((array) $keys as $key) {
651+
$results[$key] = $array[$key];
652+
}
653+
} else {
654+
foreach ((array) $keys as $key) {
655+
$results[] = $array[$key];
656656
}
657-
658-
$keys[$j] = $keys[$i];
659657
}
660658

661-
return $selected;
659+
return $results;
662660
}
663661

662+
664663
/**
665664
* Set an array item to a given value using "dot" notation.
666665
*
@@ -710,29 +709,15 @@ public static function set(&$array, $key, $value)
710709
*/
711710
public static function shuffle($array, $seed = null)
712711
{
713-
if (! is_null($seed)) {
712+
if (is_null($seed)) {
713+
shuffle($array);
714+
} else {
714715
mt_srand($seed);
715716
shuffle($array);
716717
mt_srand();
717-
718-
return $array;
719-
}
720-
721-
if (empty($array)) {
722-
return [];
723-
}
724-
725-
$keys = array_keys($array);
726-
727-
for ($i = count($keys) - 1; $i > 0; $i--) {
728-
$j = random_int(0, $i);
729-
$shuffled[] = $array[$keys[$j]];
730-
$keys[$j] = $keys[$i];
731718
}
732719

733-
$shuffled[] = $array[$keys[0]];
734-
735-
return $shuffled;
720+
return $array;
736721
}
737722

738723
/**

tests/Support/SupportArrTest.php

+1-38
Original file line numberDiff line numberDiff line change
@@ -731,19 +731,6 @@ public function testRandom()
731731
$this->assertCount(2, array_intersect_assoc(['one' => 'foo', 'two' => 'bar', 'three' => 'baz'], $random));
732732
}
733733

734-
public function testRandomIsActuallyRandom()
735-
{
736-
$values = [];
737-
738-
for ($i = 0; $i < 100; $i++) {
739-
$values[] = Arr::random(['foo', 'bar', 'baz']);
740-
}
741-
742-
$this->assertContains('foo', $values);
743-
$this->assertContains('bar', $values);
744-
$this->assertContains('baz', $values);
745-
}
746-
747734
public function testRandomNotIncrementingKeys()
748735
{
749736
$random = Arr::random(['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz']);
@@ -830,34 +817,10 @@ public function testSet()
830817

831818
public function testShuffleWithSeed()
832819
{
833-
$this->assertSame(
820+
$this->assertEquals(
834821
Arr::shuffle(range(0, 100, 10), 1234),
835822
Arr::shuffle(range(0, 100, 10), 1234)
836823
);
837-
838-
$this->assertNotSame(
839-
range(0, 100, 10),
840-
Arr::shuffle(range(0, 100, 10), 1234)
841-
);
842-
}
843-
844-
public function testShuffle()
845-
{
846-
$source = range('a', 'z'); // alphabetic keys to ensure values are returned
847-
848-
$sameElements = true;
849-
$dontMatch = false;
850-
851-
// Attempt 5x times to prevent random failures
852-
for ($i = 0; $i < 5; $i++) {
853-
$shuffled = Arr::shuffle($source);
854-
855-
$dontMatch = $dontMatch || $source !== $shuffled;
856-
$sameElements = $sameElements && $source === array_values(Arr::sort($shuffled));
857-
}
858-
859-
$this->assertTrue($sameElements, 'Shuffled array should always have the same elements.');
860-
$this->assertTrue($dontMatch, 'Shuffled array should not have the same order.');
861824
}
862825

863826
public function testEmptyShuffle()

0 commit comments

Comments
 (0)