golang 打乱数组顺序
原创
©著作权归作者所有:来自51CTO博客作者angdh的原创作品,请联系作者获取转载授权,否则将追究法律责任
package main
import (
"fmt"
"math/rand"
"time"
)
func MicsSlice(origin []int64, count int) []int64 {
tmpOrigin := make([]int64, len(origin))
copy(tmpOrigin, origin)
rand.Seed(time.Now().Unix())
rand.Shuffle(len(tmpOrigin), func(i int, j int) {
tmpOrigin[i], tmpOrigin[j] = tmpOrigin[j], tmpOrigin[i]
})
fmt.Println("result....", tmpOrigin)
result := make([]int64, 0, count)
for index, value := range tmpOrigin {
if index == count {
break
}
result = append(result, value)
}
return result
}
func main() {
dest := MicsSlice([]int64{1, 2, 3, 4, 5, 6, 73, 2}, 2)
fmt.Println("result....", dest)
}