Created
December 30, 2013 22:24
-
-
Save CurtisHumphrey/8189277 to your computer and use it in GitHub Desktop.
Fisher-Yates shuffle (in-place) in coffeescript
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
### | |
Randomize array element order in-place. | |
Using Fisher-Yates shuffle algorithm. | |
### | |
shuffleArray = (array) -> | |
i = array.length - 1 | |
while i > 0 | |
j = Math.floor(Math.random() * (i + 1)) | |
temp = array[i] | |
array[i] = array[j] | |
array[j] = temp | |
i-- | |
array |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment