Last active
December 16, 2015 18:08
-
-
Save ischenkodv/5475151 to your computer and use it in GitHub Desktop.
Knuth 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
// Implementation of Knuth shuffle: http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle | |
// More implementations: http://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array-in-javascript | |
var shuffle = function(a) { | |
for (var i = a.length; --i > 0;) { | |
// Get random number between first and current index. | |
var r = Math.floor(Math.random() * (i + 1)); | |
// Swap random with current index values. | |
var d = a[r]; | |
a[r] = a[i]; | |
a[i] = d; | |
} | |
return a; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment