Skip to content

Instantly share code, notes, and snippets.

@C-Rodg
Last active May 14, 2018 23:29
Show Gist options
  • Save C-Rodg/9b890177c1416828110a5c50a5bf5d75 to your computer and use it in GitHub Desktop.
Save C-Rodg/9b890177c1416828110a5c50a5bf5d75 to your computer and use it in GitHub Desktop.
The Fisher-Yates (Knuth) Shuffle implemented in Javascript.
const shuffle = (arr) => {
let currentIndex = arr.length;
let tempValue, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
tempValue = arr[currentIndex];
arr[currentIndex] = arr[randomIndex];
arr[randomIndex] = tempValue;
}
return arr;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment