shuffle.js 585 B

12345678910111213141516171819202122232425262728
  1. var randInt = require('../random/randInt');
  2. /**
  3. * Shuffle array items.
  4. */
  5. function shuffle(arr) {
  6. var results = [],
  7. rnd;
  8. if (arr == null) {
  9. return results;
  10. }
  11. var i = -1, len = arr.length, value;
  12. while (++i < len) {
  13. if (!i) {
  14. results[0] = arr[0];
  15. } else {
  16. rnd = randInt(0, i);
  17. results[i] = results[rnd];
  18. results[rnd] = arr[i];
  19. }
  20. }
  21. return results;
  22. }
  23. module.exports = shuffle;