Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9f409d2

Browse files
committed
Enhance array shuffle method to support custom random engines
1 parent868d333 commit9f409d2

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

‎src/collection/src/Arr.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
useHyperf\Macroable\Macroable;
1818
useHyperf\Stringable\Str;
1919
useInvalidArgumentException;
20+
useRandom\Engine;
21+
useRandom\Engine\Mt19937;
22+
useRandom\Randomizer;
2023

2124
/**
2225
* @template TKey of array-key
@@ -472,22 +475,21 @@ public static function set(array &$array, null|int|string $key, mixed $value): a
472475
/**
473476
* Shuffle the given array and return the result.
474477
*/
475-
publicstaticfunctionshuffle(array$array,?int$seed =null):array
478+
publicstaticfunctionshuffle(array$array,null|Engine|int$seed =null):array
476479
{
477480
if (empty($array)) {
478481
return [];
479482
}
480483

481-
if (!is_null($seed)) {
482-
mt_srand($seed);
483-
shuffle($array);
484-
mt_srand();
485-
return$array;
486-
}
484+
$engine =match (true) {
485+
$seedinstanceof Engine =>$seed,
486+
is_int($seed) =>newMt19937($seed),
487+
default =>null,
488+
};
487489

488-
shuffle($array);
490+
$randomizer =newRandomizer($engine);
489491

490-
return$array;
492+
return$randomizer->shuffleArray($array);
491493
}
492494

493495
/**

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp