terraform-provider-google/vendor/github.com/terraform-providers/terraform-provider-random/random/seed.go
Riley Karson 8c0bc93771
Vendor random provider to allow use in tests (#2110)
Support for an upcoming generated PR.
2018-09-25 15:09:13 -07:00

25 lines
529 B
Go

package random
import (
"hash/crc64"
"math/rand"
"time"
)
// NewRand returns a seeded random number generator, using a seed derived
// from the provided string.
//
// If the seed string is empty, the current time is used as a seed.
func NewRand(seed string) *rand.Rand {
var seedInt int64
if seed != "" {
crcTable := crc64.MakeTable(crc64.ISO)
seedInt = int64(crc64.Checksum([]byte(seed), crcTable))
} else {
seedInt = time.Now().UnixNano()
}
randSource := rand.NewSource(seedInt)
return rand.New(randSource)
}