Skip to content

Instantly share code, notes, and snippets.

@thenoviceoof
Created July 2, 2012 05:14
Show Gist options
  • Save thenoviceoof/3031220 to your computer and use it in GitHub Desktop.
Save thenoviceoof/3031220 to your computer and use it in GitHub Desktop.
Fisher-Yates shuffle in js
// using http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
function randrange(i) {
return Math.floor(i*Math.random());
}
// in-place shuffle
function shuffle(items) {
var tmp;
var i = items.length - 1;
while(i >= 1) {
j = randrange(i);
tmp = items[i];
items[i] = items[j];
items[j] = tmp;
i -= 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment