Last active
July 16, 2024 08:29
-
-
Save jonrandy/d97d8fd17849c2069d9a2447b8e99c9b to your computer and use it in GitHub Desktop.
Fisher Yates Shuffle
This file contains 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
function shuffle(array) { | |
let a = [...array], m = a.length, i | |
while (m) { | |
// Pick a remaining element | |
i = ~~(Math.random() * m--); | |
// Swap it with the current element | |
[a[m], a[i]] = [a[i], a[m]] | |
} | |
return a | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment