Created
July 12, 2012 13:29
-
-
Save smrchy/3098096 to your computer and use it in GitHub Desktop.
Fisher-Yates shuffle in Coffeescript
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
| # Fisher-Yates shuffle in Coffeescript | |
| # | |
| # _shuffle(array) | |
| # | |
| # Returns a shuffled version of the array leaving the original untouched. | |
| _shuffle = (obj) -> | |
| shuffled = [] | |
| for e, i in obj | |
| if i is 0 | |
| shuffled[0] = e | |
| else | |
| rand = Math.floor(Math.random() * (i + 1)) | |
| shuffled[i] = shuffled[rand] | |
| shuffled[rand] = e | |
| shuffled |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment