Skip to content

Instantly share code, notes, and snippets.

@Basemm
Last active August 29, 2015 13:55
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 Basemm/8789091 to your computer and use it in GitHub Desktop.
Save Basemm/8789091 to your computer and use it in GitHub Desktop.
Array Shuffle
arrayShuffle = (arr, numOfSwaps = arr.length / 2) ->
#copy array
newArr = arr.slice(0)
for i in [0...numOfSwaps]
rnd1 = Math.floor( Math.random() * arr.length )
rnd2 = Math.floor( Math.random() * arr.length )
#swap elements
buf = newArr[rnd1]
newArr[rnd1] = newArr[rnd2]
newArr[rnd2] = buf
return newArr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment