Skip to content

Instantly share code, notes, and snippets.

@duhaime
Created December 11, 2018 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duhaime/2c2510d535063e8d1b5c02e1d7cb31b4 to your computer and use it in GitHub Desktop.
Save duhaime/2c2510d535063e8d1b5c02e1d7cb31b4 to your computer and use it in GitHub Desktop.
Fisher-Yates Shuffle
function shuffle(l) {
var plucked = [],
len = l.length;
while (plucked.length < len) {
var idx = parseInt(Math.random() * l.length);
plucked.push( l.splice(idx, 1)[0] );
}
return plucked;
}
var l = [1,2,3,4,5];
l = shuffle(l);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment