Last active
August 29, 2015 14:02
-
-
Save magicznyleszek/9bbb7f75464fbbea87f0 to your computer and use it in GitHub Desktop.
Fisher-Yates shuffle algorithm
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
// Fisher-Yates shuffle algorithm: http://bost.ocks.org/mike/shuffle/ | |
function shuffle(array) { | |
var m = array.length, t, i; | |
// while there remain elements to shuffle | |
while (m) { | |
// pick a remaining element | |
i = Math.floor(Math.random() * m--); | |
// and swap it with the current element | |
t = array[m]; | |
array[m] = array[i]; | |
array[i] = t; | |
} | |
return array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment