github.com/lmb/consul@v1.4.1/lib/rand.go (about) 1 package lib 2 3 import ( 4 crand "crypto/rand" 5 "math" 6 "math/big" 7 "math/rand" 8 "sync" 9 "time" 10 ) 11 12 var ( 13 once sync.Once 14 15 // SeededSecurely is set to true if a cryptographically secure seed 16 // was used to initialize rand. When false, the start time is used 17 // as a seed. 18 SeededSecurely bool 19 ) 20 21 // SeedMathRand provides weak, but guaranteed seeding, which is better than 22 // running with Go's default seed of 1. A call to SeedMathRand() is expected 23 // to be called via init(), but never a second time. 24 func SeedMathRand() { 25 once.Do(func() { 26 n, err := crand.Int(crand.Reader, big.NewInt(math.MaxInt64)) 27 if err != nil { 28 rand.Seed(time.Now().UTC().UnixNano()) 29 return 30 } 31 rand.Seed(n.Int64()) 32 SeededSecurely = true 33 }) 34 }