Skip to content

Instantly share code, notes, and snippets.

@wedneyyuri
Created August 25, 2015 23:19
Show Gist options
  • Save wedneyyuri/0d528c26b82bfcb6a437 to your computer and use it in GitHub Desktop.
Save wedneyyuri/0d528c26b82bfcb6a437 to your computer and use it in GitHub Desktop.
UnderscoreJS lightweight method: shuffle
var shuffle = (function(){
var random = function(min, max) {
return min + Math.floor(Math.random() * (max - min + 1));
};
return function(set) {
var length = set.length;
var shuffled = Array(length);
for (var index = 0, rand; index < length; index++) {
rand = random(0, index);
if (rand !== index) shuffled[index] = shuffled[rand];
shuffled[rand] = set[index];
}
return shuffled;
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment