Created
May 24, 2012 18:37
-
-
Save matths/2783365 to your computer and use it in GitHub Desktop.
Fisher Yates shuffle of a jQuery selection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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