Random Seeded Array Shuffle


/ Published in: PHP
Save to your folder(s)

Repeatable Fisher Yates Array Shuffle based on given random string. If no seed is provided the current second will be used (not to use as a pure random shuffle... based on microseconds)


Copy this code and paste it in your HTML
  1. function fyshuffle(&$items,$seedstring) {
  2. if (!$seedstring) {
  3. $seedval = time();
  4. } else {
  5. if (is_numeric($seedstring)) {
  6. $seedval = $seedstring;
  7. } else {
  8. for($i=0;$i<=strlen($seedstring);$i++) {
  9. $seedval += ord($seedstring[$i]);
  10. }
  11. }
  12.  
  13. srand($seedval);
  14. for ($i = count($items) - 1; $i > 0; $i--) {
  15. $j = @rand(0, $i);
  16. $tmp = $items[$i];
  17. $items[$i] = $items[$j];
  18. $items[$j] = $tmp;
  19. }
  20. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.