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

Commit8923f0a

Browse files
committed
Enhance array shuffle method to support custom random engines
1 parentcf24ee1 commit8923f0a

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
@@ -502,22 +505,21 @@ public static function set(array &$array, null|int|string $key, mixed $value): a
502505
/**
503506
* Shuffle the given array and return the result.
504507
*/
505-
publicstaticfunctionshuffle(array$array,?int$seed =null):array
508+
publicstaticfunctionshuffle(array$array,null|Engine|int$seed =null):array
506509
{
507510
if (empty($array)) {
508511
return [];
509512
}
510513

511-
if (!is_null($seed)) {
512-
mt_srand($seed);
513-
shuffle($array);
514-
mt_srand();
515-
return$array;
516-
}
514+
$engine =match (true) {
515+
$seedinstanceof Engine =>$seed,
516+
is_int($seed) =>newMt19937($seed),
517+
default =>null,
518+
};
517519

518-
shuffle($array);
520+
$randomizer =newRandomizer($engine);
519521

520-
return$array;
522+
return$randomizer->shuffleArray($array);
521523
}
522524

523525
/**

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp