数组乱序
方法1.sliceOutOfOrder 选定随机数r 将下标r和下边i交换,i– ,就是和当前最后一位交换
方法2.rand.Perm(数组长度) 将原数组下标按照随机出来的数据赋值给另一数组
package main
import "fmt"
import "math/rand"
import "time"
var (
rr = rand.New(rand.NewSource(time.Now().UnixNano()))
a1 = [2]int{}
a2 = [10]int{}
)
func main() {
a := []int{0,1}
for i := 0; i < 10000; i++ {
randslice()
//fmt.Println(rr.Intn(10))
sliceOutOfOrder(a)
}
fmt.Println(a1)
fmt.Println(a2)
}
func sliceOutOfOrder(in []int) []int {
l := len(in)
for i := l - 1; i > 0; i-- {
r := rr.Intn(i)
in[r],in[i] = in[i],in[r]
}
a1[in[0]] += 1
return in
}
func randslice() {
in := rr.Perm(10)
a2[in[0]] += 1
}