Skip to content

Instantly share code, notes, and snippets.

@ischenkodv
Last active December 16, 2015 18:08
Show Gist options
  • Save ischenkodv/5475151 to your computer and use it in GitHub Desktop.
Save ischenkodv/5475151 to your computer and use it in GitHub Desktop.
Knuth shuffle
// 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