Skip to content

Instantly share code, notes, and snippets.

@mass6
Last active November 29, 2017 14:23
Show Gist options
  • Save mass6/c3be9dff1a55d0913599d603d15e8d82 to your computer and use it in GitHub Desktop.
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.
<?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