mirror of
https://github.com/pyihe/go-pkg.git
synced 2025-10-05 16:06:58 +08:00
feature(rands): fix shuffle
This commit is contained in:
@@ -39,20 +39,17 @@ func SafeString(n int) (s string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// ShuffleBytes shuffle 随机算法
|
func Shuffle(n int, swap func(i, j int)) {
|
||||||
func ShuffleBytes(data []byte) {
|
if n < 0 {
|
||||||
count := len(data)
|
panic("invalid n")
|
||||||
for i := 0; i < count; i++ {
|
}
|
||||||
pos := rad.Intn(count-i) + i
|
i := n - 1
|
||||||
data[i], data[pos] = data[pos], data[i]
|
for ; i > 1<<31-1-1; i-- {
|
||||||
}
|
j := int(rad.Int63n(int64(i + 1)))
|
||||||
}
|
swap(i, j)
|
||||||
|
}
|
||||||
// ShuffleInt shuffle int
|
for ; i > 0; i-- {
|
||||||
func ShuffleInt(data []int) {
|
j := int(rad.Int31n(int32(i + 1)))
|
||||||
count := len(data)
|
swap(i, j)
|
||||||
for i := 0; i < count; i++ {
|
|
||||||
pos := rad.Intn(count-i) + i
|
|
||||||
data[i], data[pos] = data[pos], data[i]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user