Fix bug: rand.Seed reset and generate duplicate number (#5361)

Signed-off-by: zhenshan.cao <zhenshan.cao@zilliz.com>
pull/5358/head^2
zhenshan.cao 2021-05-24 10:50:37 +08:00 committed by GitHub
parent 457e964e41
commit 4e1b12269b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 2 deletions

View File

@ -16,8 +16,10 @@ import (
"time"
)
var Rand *rand.Rand = nil
func init() {
rand.Seed(time.Now().UnixNano())
Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
}
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
@ -25,7 +27,7 @@ var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func RandomString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
b[i] = letterRunes[Rand.Intn(len(letterRunes))]
}
return string(b)
}