tijl.dev-core/internal/apps/flags/util.go

39 lines
754 B
Go
Raw Normal View History

2024-08-26 14:22:24 +02:00
package flags
import (
"crypto/sha256"
"encoding/binary"
"math/rand"
)
func shuffleSliceRandom(slice []string) []string {
shuffled := make([]string, len(slice))
copy(shuffled, slice)
for i := len(shuffled) - 1; i > 0; i-- {
j := rand.Intn(i + 1)
shuffled[i], shuffled[j] = shuffled[j], shuffled[i]
}
return shuffled
}
func shuffleSlice(slice []string, seed string) []string {
hasher := sha256.New()
hasher.Write([]byte(seed))
hash := hasher.Sum(nil)
seedInt := int64(binary.LittleEndian.Uint64(hash[:8]))
r := rand.New(rand.NewSource(seedInt))
shuffled := make([]string, len(slice))
copy(shuffled, slice)
r.Shuffle(len(shuffled), func(i, j int) {
shuffled[i], shuffled[j] = shuffled[j], shuffled[i]
})
return shuffled
}