From ec161f335d631a0ac41ab5f3d89715a8929a1db4 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Thu, 14 Nov 2024 16:27:22 +0800 Subject: [PATCH] fix: update random seed when call random function --- random/random.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/random/random.go b/random/random.go index 1aa5db7..730ea9a 100644 --- a/random/random.go +++ b/random/random.go @@ -10,6 +10,7 @@ import ( "io" "math" "math/rand" + "os" "time" "unsafe" @@ -274,7 +275,9 @@ func nearestPowerOfTwo(cap int) int { // random generate a random string based on given string range. func random(s string, length int) string { // 确保随机数生成器的种子是动态的 - rand.Seed(time.Now().UnixNano()) + pid := os.Getpid() + timestamp := time.Now().UnixNano() + rand.Seed(int64(pid) + timestamp) // 仿照strings.Builder // 创建一个长度为 length 的字节切片