Last active
November 29, 2017 14:23
-
-
Save mass6/c3be9dff1a55d0913599d603d15e8d82 to your computer and use it in GitHub Desktop.
Creates a repeatable randomizer/shuffle of an array based on a given seed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tattoodo\Helpers; | |
use Carbon\Carbon; | |
/** | |
* Creates a repeatable randomizer/shuffle of an array based on a given seed. | |
*/ | |
class Randomizer | |
{ | |
/** | |
* @param array $items | |
* @param int|null $seed | |
* | |
* @return array | |
*/ | |
public static function shuffle(array $items, int $seed = null): array | |
{ | |
$seed = $seed ?? Carbon::today()->timestamp; | |
return collect($items)->map(function($item, $key) use ($seed) { | |
return [md5($item . $seed) => $item]; | |
})->collapse()->sortBy(function ($item, $key) { | |
return $key; | |
})->values(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment