Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@CurtisHumphrey
Created December 30, 2013 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save CurtisHumphrey/8189277 to your computer and use it in GitHub Desktop.
Save CurtisHumphrey/8189277 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.
###
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