Skip to content

Commit

Permalink
Use local rand.Rand instance instead of package global rand.Seed()
Browse files Browse the repository at this point in the history
  • Loading branch information
stoewer committed Dec 11, 2023
1 parent c3e782b commit ce5a657
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions pkg/random/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ var (
resources = []string{
"order", "payment", "customer", "product", "stock", "inventory",
"shipping", "billing", "checkout", "cart", "search", "analytics"}

rnd *rand.Rand
)

func init() {
seed, _ := crand.Int(crand.Reader, big.NewInt(int64(^uint64(0)>>1)))
rand.Seed(seed.Int64())
rnd = rand.New(rand.NewSource(seed.Int64()))
}

func SelectElement[T any](elements []T) T {
return elements[rand.Intn(len(elements))]
return elements[rnd.Intn(len(elements))]
}

func String(n int) string {
Expand All @@ -47,17 +49,17 @@ func K6String(n int) string {
}

func IntBetween(min, max int) int {
n := rand.Intn(max - min)
n := rnd.Intn(max - min)
return min + n
}

func Duration(min, max time.Duration) time.Duration {
n := rand.Int63n(int64(max) - int64(min))
n := rnd.Int63n(int64(max) - int64(min))
return min + time.Duration(n)
}

func IPAddr() string {
return fmt.Sprintf("192.168.%d.%d", rand.Intn(255), rand.Intn(255))
return fmt.Sprintf("192.168.%d.%d", rnd.Intn(255), rnd.Intn(255))
}

func Port() int {
Expand Down Expand Up @@ -110,12 +112,12 @@ func OperationForResource(resource string) string {

func TraceID() pcommon.TraceID {
var b [16]byte
_, _ = rand.Read(b[:]) // always returns nil error
_, _ = rnd.Read(b[:]) // always returns nil error
return b
}

func SpanID() pcommon.SpanID {
var b [8]byte
_, _ = rand.Read(b[:]) // always returns nil error
_, _ = rnd.Read(b[:]) // always returns nil error
return b
}

0 comments on commit ce5a657

Please sign in to comment.