From 4e1b12269b1435ee7bf9c306e5c0ce5a5fc13584 Mon Sep 17 00:00:00 2001 From: "zhenshan.cao" Date: Mon, 24 May 2021 10:50:37 +0800 Subject: [PATCH] Fix bug: rand.Seed reset and generate duplicate number (#5361) Signed-off-by: zhenshan.cao --- internal/util/funcutil/random.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/internal/util/funcutil/random.go b/internal/util/funcutil/random.go index ad3f5f8682..2159eaeb0f 100644 --- a/internal/util/funcutil/random.go +++ b/internal/util/funcutil/random.go @@ -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) }