diff --git a/lib/rand.go b/lib/rand.go new file mode 100644 index 000000000..48307e63f --- /dev/null +++ b/lib/rand.go @@ -0,0 +1,18 @@ +package lib + +import ( + "math/rand" + "sync" + "time" +) + +var ( + once sync.Once +) + +// SeedMathRand provides weak, but guaranteed seeding, which is better than +// running with Go's default seed of 1. A call to SeedMathRand() is expected +// to be called via init(), but never a second time. +func SeedMathRand() { + once.Do(func() { rand.Seed(time.Now().UTC().UnixNano()) }) +} diff --git a/main.go b/main.go index 70b134359..249e6e253 100644 --- a/main.go +++ b/main.go @@ -6,8 +6,14 @@ import ( "io/ioutil" "log" "os" + + "github.com/hashicorp/consul/lib" ) +func init() { + lib.SeedMathRand() +} + func main() { os.Exit(realMain()) }