Skip to content

Commit

Permalink
Merge pull request #21710 from hashicorp/jbardin/acctest-random
Browse files Browse the repository at this point in the history
only seed math/rand once
  • Loading branch information
jbardin authored Jun 13, 2019
2 parents 3938392 + a036ea0 commit edd005c
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions helper/acctest/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,25 @@ import (
"golang.org/x/crypto/ssh"
)

func init() {
rand.Seed(time.Now().UTC().UnixNano())
}

// Helpers for generating random tidbits for use in identifiers to prevent
// collisions in acceptance tests.

// RandInt generates a random integer
func RandInt() int {
reseed()
return rand.New(rand.NewSource(time.Now().UnixNano())).Int()
}

// RandomWithPrefix is used to generate a unique name with a prefix, for
// randomizing names in acceptance tests
func RandomWithPrefix(name string) string {
reseed()
return fmt.Sprintf("%s-%d", name, rand.New(rand.NewSource(time.Now().UnixNano())).Int())
}

func RandIntRange(min int, max int) int {
reseed()
source := rand.New(rand.NewSource(time.Now().UnixNano()))
rangeMax := max - min

Expand All @@ -48,7 +49,6 @@ func RandString(strlen int) string {
// RandStringFromCharSet generates a random string by selecting characters from
// the charset provided
func RandStringFromCharSet(strlen int, charSet string) string {
reseed()
result := make([]byte, strlen)
for i := 0; i < strlen; i++ {
result[i] = charSet[rand.Intn(len(charSet))]
Expand Down Expand Up @@ -129,11 +129,6 @@ func pemEncode(b []byte, block string) (string, error) {
return buf.String(), nil
}

// Seeds random with current timestamp
func reseed() {
rand.Seed(time.Now().UTC().UnixNano())
}

const (
// CharSetAlphaNum is the alphanumeric character set for use with
// RandStringFromCharSet
Expand Down

0 comments on commit edd005c

Please sign in to comment.