github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/random/seed.go (about)

     1  package random
     2  
     3  import (
     4  	"hash/crc64"
     5  	"math/rand"
     6  	"time"
     7  )
     8  
     9  // NewRand returns a seeded random number generator, using a seed derived
    10  // from the provided string.
    11  //
    12  // If the seed string is empty, the current time is used as a seed.
    13  func NewRand(seed string) *rand.Rand {
    14  	var seedInt int64
    15  	if seed != "" {
    16  		crcTable := crc64.MakeTable(crc64.ISO)
    17  		seedInt = int64(crc64.Checksum([]byte(seed), crcTable))
    18  	} else {
    19  		seedInt = time.Now().Unix()
    20  	}
    21  
    22  	randSource := rand.NewSource(seedInt)
    23  	return rand.New(randSource)
    24  }