Created
August 25, 2015 23:19
-
-
Save wedneyyuri/0d528c26b82bfcb6a437 to your computer and use it in GitHub Desktop.
UnderscoreJS lightweight method: shuffle
This file contains hidden or 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
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