Fix rand source in name (#3070)

This commit is contained in:
Stavros Kontopoulos 2024-06-26 16:41:49 +03:00 committed by GitHub
parent ee1db869c7
commit 3f6a546ac3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 1 deletions

View File

@ -19,6 +19,7 @@ package helpers
import (
"math/rand"
"strings"
"sync"
"time"
"unicode"
)
@ -32,7 +33,10 @@ const (
testNamePrefix = "Test"
)
var nameRand *rand.Rand
var (
nameRand *rand.Rand
nameMu sync.Mutex
)
func init() {
// Properly seed the random number generator so RandomString() is actually random.
@ -75,9 +79,11 @@ func AppendRandomString(prefix string) string {
// RandomString will generate a random string.
func RandomString() string {
suffix := make([]byte, randSuffixLen)
nameMu.Lock()
for i := range suffix {
suffix[i] = letterBytes[nameRand.Intn(len(letterBytes))]
}
nameMu.Unlock()
return string(suffix)
}