Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save undecided/40c580e55f48449d38ff to your computer and use it in GitHub Desktop.
Save undecided/40c580e55f48449d38ff to your computer and use it in GitHub Desktop.
Fisher-Yates shuffle (in-place) in coffeescript
###
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