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