From c3e471b7a19354a27a470878e78d0d5ec599007b Mon Sep 17 00:00:00 2001 From: git-hulk Date: Sun, 3 Sep 2023 13:08:23 +0800 Subject: [PATCH 1/3] Replace the random char seed to global rand to avoid collision --- tests/gocase/util/random.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/gocase/util/random.go b/tests/gocase/util/random.go index 098e9df3929..f4f0351e2ba 100644 --- a/tests/gocase/util/random.go +++ b/tests/gocase/util/random.go @@ -75,7 +75,8 @@ func RandStringWithSeed(min, max int, typ RandStringType, seed int64) string { var sb strings.Builder for ; length > 0; length-- { - s := fmt.Sprintf("%c", minVal+int(r.Int31n(int32(maxVal-minVal+1)))) + // use global rand to avoid collision + s := fmt.Sprintf("%c", minVal+rand.Intn(maxVal-minVal+1)) sb.WriteString(s) } return sb.String() From 0b21298f6d260e3f8d19c06978a3ffca4f9f987a Mon Sep 17 00:00:00 2001 From: git-hulk Date: Sun, 3 Sep 2023 15:36:47 +0800 Subject: [PATCH 2/3] Use rand.Int63 as the random seed --- tests/gocase/util/random.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/gocase/util/random.go b/tests/gocase/util/random.go index f4f0351e2ba..22acf95389c 100644 --- a/tests/gocase/util/random.go +++ b/tests/gocase/util/random.go @@ -23,7 +23,6 @@ import ( "fmt" "math/rand" "strings" - "time" ) func RandPath[T any](f ...func() T) T { @@ -58,7 +57,7 @@ const ( ) func RandString(min, max int, typ RandStringType) string { - return RandStringWithSeed(min, max, typ, time.Now().UnixNano()) + return RandStringWithSeed(min, max, typ, rand.Int63()) } func RandStringWithSeed(min, max int, typ RandStringType, seed int64) string { @@ -76,7 +75,7 @@ func RandStringWithSeed(min, max int, typ RandStringType, seed int64) string { var sb strings.Builder for ; length > 0; length-- { // use global rand to avoid collision - s := fmt.Sprintf("%c", minVal+rand.Intn(maxVal-minVal+1)) + s := fmt.Sprintf("%c", minVal+r.Intn(maxVal-minVal+1)) sb.WriteString(s) } return sb.String() From 9d420df1afb34d846ca8e13dd191f9dcccbc5bad Mon Sep 17 00:00:00 2001 From: hulk Date: Sun, 3 Sep 2023 16:00:19 +0800 Subject: [PATCH 3/3] Update tests/gocase/util/random.go --- tests/gocase/util/random.go | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/gocase/util/random.go b/tests/gocase/util/random.go index 22acf95389c..15acc4b8c75 100644 --- a/tests/gocase/util/random.go +++ b/tests/gocase/util/random.go @@ -74,7 +74,6 @@ func RandStringWithSeed(min, max int, typ RandStringType, seed int64) string { var sb strings.Builder for ; length > 0; length-- { - // use global rand to avoid collision s := fmt.Sprintf("%c", minVal+r.Intn(maxVal-minVal+1)) sb.WriteString(s) }