subsonic-tui/internal/common/shuffle.go
Sagi Dayan bae2c74815
initial commit
Signed-off-by: Sagi Dayan <sagidayan@gmail.com>
2024-12-14 23:23:29 +02:00

21 lines
360 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()))
}
}