Skip to content

Instantly share code, notes, and snippets.

@danielhaim1
Created March 19, 2023 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielhaim1/b9ba016ff1cbe154a87dddec99aa3f17 to your computer and use it in GitHub Desktop.
Save danielhaim1/b9ba016ff1cbe154a87dddec99aa3f17 to your computer and use it in GitHub Desktop.
Knuth Shuffle algorithm
function knuthShuffle(arr) {
let currentIndex = arr.length;
let temporaryValue, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
temporaryValue = arr[currentIndex];
arr[currentIndex] = arr[randomIndex];
arr[randomIndex] = temporaryValue;
}
return arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment