Created
February 18, 2019 03:02
-
-
Save adamcrampton/7fab9e69e0935150ce990a7d0e99e40e to your computer and use it in GitHub Desktop.
Randomize a JS object using Fisher-Yates-Durstenfeld 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-Durstenfeld shuffle | |
// https://stackoverflow.com/questions/3718282/javascript-shuffling-objects-inside-an-object-randomize | |
function shuffle(sourceArray) { | |
for (var i = 0; i < sourceArray.length - 1; i++) { | |
var j = i + Math.floor(Math.random() * (sourceArray.length - i)); | |
var temp = sourceArray[j]; | |
sourceArray[j] = sourceArray[i]; | |
sourceArray[i] = temp; | |
} | |
return sourceArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment