Skip to content

Instantly share code, notes, and snippets.

@jonrandy
Last active July 16, 2024 08:29
Show Gist options
  • Save jonrandy/d97d8fd17849c2069d9a2447b8e99c9b to your computer and use it in GitHub Desktop.
Save jonrandy/d97d8fd17849c2069d9a2447b8e99c9b to your computer and use it in GitHub Desktop.
Fisher Yates Shuffle
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