Created
April 1, 2014 03:26
-
-
Save gfcarvalho/9907168 to your computer and use it in GitHub Desktop.
Shuffles a given array using an implemented version of Fisher–Yates algorithm
This file contains 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
function shuffle(array) { | |
var counter = array.length, temp, index; | |
// While there are elements in the array | |
while (counter--) { | |
// Pick a random index | |
index = (Math.random() * (counter+1)) | 0; | |
// And swap the last element with it | |
temp = array[counter]; | |
array[counter] = array[index]; | |
array[index] = temp; | |
} | |
return array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fisher–Yates shuffle reference