Last active
May 14, 2018 23:29
-
-
Save C-Rodg/9b890177c1416828110a5c50a5bf5d75 to your computer and use it in GitHub Desktop.
The Fisher-Yates (Knuth) Shuffle implemented in Javascript.
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
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