Last active
August 29, 2015 13:55
-
-
Save Basemm/8789091 to your computer and use it in GitHub Desktop.
Array Shuffle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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