Created
November 17, 2010 21:15
-
-
Save keeganwatkins/704103 to your computer and use it in GitHub Desktop.
Simple mechanism for randomizing the contents of an Array
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
var shuffle = function(source) { | |
var ret = Array.prototype.slice.call(source), | |
len = source.length, | |
i = len, | |
rand, temp; | |
while (i--) { | |
rand = parseInt( Math.random() * len, 10 ); | |
temp = ret[ i ]; | |
ret[ i ] = ret[ rand ]; | |
ret[ rand ] = temp; | |
} | |
return ret; | |
}; | |
// Usage: | |
shuffle( ['a', 'b', 'c', 'd'] ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment