Last active
December 31, 2017 03:43
-
-
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
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
// **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; | |
} |
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
// 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