Skip to content

Instantly share code, notes, and snippets.

@beatak
Created May 25, 2012 20:02
Show Gist options
  • Save beatak/2790267 to your computer and use it in GitHub Desktop.
Save beatak/2790267 to your computer and use it in GitHub Desktop.
Fisher-Yates
// http://stackoverflow.com/questions/962802/is-it-correct-to-use-javascript-array-sort-method-for-shuffling
Array.prototype.shuffle = function () {
var tmp, current, top = this.length;
if (top) {
while(--top) {
current = Math.floor(Math.random() * (top + 1));
tmp = this[current];
this[current] = this[top];
this[top] = tmp;
}
}
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment