Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save undecided/40c580e55f48449d38ff to your computer and use it in GitHub Desktop.

Select an option

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