Skip to content

Instantly share code, notes, and snippets.

@smrchy
Created July 12, 2012 13:29
Show Gist options
  • Select an option

  • Save smrchy/3098096 to your computer and use it in GitHub Desktop.

Select an option

Save smrchy/3098096 to your computer and use it in GitHub Desktop.
Fisher-Yates shuffle in Coffeescript
# 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