Skip to content

Instantly share code, notes, and snippets.

@adamcrampton
Created February 18, 2019 03:02
Show Gist options
  • Save adamcrampton/7fab9e69e0935150ce990a7d0e99e40e to your computer and use it in GitHub Desktop.
Save adamcrampton/7fab9e69e0935150ce990a7d0e99e40e to your computer and use it in GitHub Desktop.
Randomize a JS object using Fisher-Yates-Durstenfeld shuffle algorithm
// 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