From 9ec15ad2ca5fccf83d705c3378286ac39b8af152 Mon Sep 17 00:00:00 2001 From: John Date: Wed, 10 Apr 2019 18:33:12 +0800 Subject: [PATCH] fix issue in repeated rand value of grand.Intn --- g/util/grand/grand_intn.go | 13 +++++++++---- geg/other/test.go | 13 ++++++++----- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/g/util/grand/grand_intn.go b/g/util/grand/grand_intn.go index de8d6244c..9677a433e 100644 --- a/g/util/grand/grand_intn.go +++ b/g/util/grand/grand_intn.go @@ -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 } } } diff --git a/geg/other/test.go b/geg/other/test.go index 20c4a87fc..63996a987 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -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)) + } + } \ No newline at end of file