Forked from CurtisHumphrey/fisher_yates_shuffle.coffee
Last active
January 21, 2016 14:36
-
-
Save undecided/40c580e55f48449d38ff 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. | |
### | |
Array.prototype.shuffle = -> | |
for i in [(@length - 1) .. 0] | |
j = Math.floor(Math.random() * (i + 1)) | |
[@[i], @[j]] = [@[j], @[i]] | |
@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment