Skip to content

Instantly share code, notes, and snippets.

@matths
Created May 24, 2012 18:37
Show Gist options
  • Save matths/2783365 to your computer and use it in GitHub Desktop.
Save matths/2783365 to your computer and use it in GitHub Desktop.
Fisher Yates shuffle of a jQuery selection
// http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
$.fn.scramble = function() {
var i = this.length;
if (i > 0) {
var j;
var tmp;
while (--i) {
j = Math.floor(Math.random() * (i + 1));
t = this[j];
this[j] = this[i];
this[i] = t;
}
}
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment