github.com/erriapo/terraform@v0.6.12-0.20160203182612-0340ea72354f/helper/acctest/random.go (about)

     1  package acctest
     2  
     3  import (
     4  	"math/rand"
     5  	"time"
     6  )
     7  
     8  // Helpers for generating random tidbits for use in identifiers to prevent
     9  // collisions in acceptance tests.
    10  
    11  // RandInt generates a random integer
    12  func RandInt() int {
    13  	reseed()
    14  	return rand.New(rand.NewSource(time.Now().UnixNano())).Int()
    15  }
    16  
    17  // RandString generates a random alphanumeric string of the length specified
    18  func RandString(strlen int) string {
    19  	return RandStringFromCharSet(strlen, CharSetAlphaNum)
    20  }
    21  
    22  // RandStringFromCharSet generates a random string by selecting characters from
    23  // the charset provided
    24  func RandStringFromCharSet(strlen int, charSet string) string {
    25  	reseed()
    26  	result := make([]byte, strlen)
    27  	for i := 0; i < strlen; i++ {
    28  		result[i] = charSet[rand.Intn(len(charSet))]
    29  	}
    30  	return string(result)
    31  }
    32  
    33  // Seeds random with current timestamp
    34  func reseed() {
    35  	rand.Seed(time.Now().UTC().UnixNano())
    36  }
    37  
    38  const (
    39  	// CharSetAlphaNum is the alphanumeric character set for use with
    40  	// RandStringFromCharSet
    41  	CharSetAlphaNum = "abcdefghijklmnopqrstuvwxyz012346789"
    42  
    43  	// CharSetAlpha is the alphabetical character set for use with
    44  	// RandStringFromCharSet
    45  	CharSetAlpha = "abcdefghijklmnopqrstuvwxyz"
    46  )