subsonic-tui/internal/common/shuffle.go
Sagi Dayan 99e8e299e4
Some checks failed
ci / check for spelling errors (pull_request) Failing after 22s
ci / Make sure build does not fail (pull_request) Successful in 2m52s
ci / code quality (lint/tests) (pull_request) Has been skipped
Added ci workflow + fixed linting issues
Signed-off-by: Sagi Dayan <sagidayan@gmail.com>
2024-12-18 17:29:16 +02:00

23 lines
362 B
Go

package common
import (
"crypto/rand"
"math/big"
"reflect"
)
func ShuffleSlice(slice interface{}) {
rv := reflect.ValueOf(slice)
swap := reflect.Swapper(slice)
length := rv.Len()
for i := length - 1; i > 0; i-- {
j, err := rand.Int(rand.Reader, big.NewInt(int64(i+1)))
if err != nil {
panic("Shuffle error")
}
swap(i, int(j.Int64()))
}
}