fix issue in repeated rand value of grand.Intn

This commit is contained in:
John
2019-04-10 18:33:12 +08:00
parent 08785cb272
commit 9ec15ad2ca
2 changed files with 17 additions and 9 deletions

View File

@ -7,8 +7,9 @@
package grand
import (
"crypto/rand"
"encoding/binary"
"crypto/rand"
"encoding/binary"
"os"
)
const (
@ -27,6 +28,7 @@ func init() {
for {
if n, err := rand.Read(buffer); err != nil {
panic(err)
os.Exit(1)
} else {
// 使用缓冲区数据进行一次完整的随机数生成
for i := 0; i < n - 4; {
@ -35,9 +37,12 @@ func init() {
}
// 充分利用缓冲区数据,随机索引递增
step = int(buffer[0])%10
if step == 0 {
step = 2
}
for i := 0; i < n - 4; {
bufferChan <- binary.BigEndian.Uint32(buffer[i : i + 4])
i += step
bufferChan <- binary.BigEndian.Uint32(buffer[i : i + 4])
i += step
}
}
}

View File

@ -1,12 +1,15 @@
package main
import (
"github.com/gogf/gf/g/test/gtest"
"fmt"
"github.com/gogf/gf/g/util/grand"
)
func test() []byte {
return nil
}
func main() {
gtest.Assert(test(), nil)
for {
fmt.Println(grand.Intn(100))
}
}