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