subsonic-tui/internal/common/shuffle.go
Sagi Dayan 864d56954a
Some checks failed
ci / check for spelling errors (pull_request) Successful in 20s
ci / code quality (lint/tests) (pull_request) Failing after 1m30s
ci / Make sure build does not fail (pull_request) Has been skipped
ci / notify-fail (pull_request) Failing after 13s
Added ci workflow + fixed linting issues
Signed-off-by: Sagi Dayan <sagidayan@gmail.com>
2024-12-18 17:34:39 +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()))
}
}