Skip to content

Instantly share code, notes, and snippets.

@THEtheChad
Last active December 31, 2017 03:43
Show Gist options
  • Save THEtheChad/3b7775f1265d4e4b45db2a165561ab60 to your computer and use it in GitHub Desktop.
Save THEtheChad/3b7775f1265d4e4b45db2a165561ab60 to your computer and use it in GitHub Desktop.
Best overall shuffling algorithm (browser tests only)[https://jsperf.com/array-shuffle-comparator/9] #toolbox
// **WARNING**
// in place (by reference) reordering
// http://stackoverflow.com/a/2450976/1037948
function knuthfisheryates2(arr) {
var temp, j, i = arr.length;
while (--i) {
j = ~~(Math.random() * (i + 1));
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
return arr;
}
// http://stackoverflow.com/a/2450976/1037948
function knuthfisheryates2(arr) {
var j, i = arr.length, next = new Array(i);
while (--i) {
j = ~~(Math.random() * (i + 1));
next[i] = arr[j];
}
return next;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment