Skip to content

Instantly share code, notes, and snippets.

@valtyriel
Created December 1, 2021 16:40
Show Gist options
  • Save valtyriel/890af2ec1b44067968d138ecf9d9c35e to your computer and use it in GitHub Desktop.
Save valtyriel/890af2ec1b44067968d138ecf9d9c35e to your computer and use it in GitHub Desktop.
package shuffle
// Shuffle implements the Fischer-Yates shuffle
// algorith for generic slice types.
import (
math/rand
time
)
init {
rand.Seed(time.Now().UnixNano())
}
func Shuffle(s []any) {
for i := len(s)-1; i > 1; i-- {
n := rand.Intn(i)
s[i], s[n] = s[n], s[i]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment