Seed math/rand more efficiently
This commit is contained in:
34
lib/rand.go
Normal file
34
lib/rand.go
Normal file
@ -0,0 +1,34 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
crand "crypto/rand"
|
||||
"math"
|
||||
"math/big"
|
||||
"math/rand"
|
||||
"sync"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var (
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
// SeedMathRand Credit: https://github.com/hashicorp/consul/blob/main/lib/rand.go
|
||||
func SeedMathRand() {
|
||||
var (
|
||||
n *big.Int
|
||||
err error
|
||||
)
|
||||
|
||||
once.Do(func() {
|
||||
n, err = crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
|
||||
if err != nil {
|
||||
log.Errorf("cannot seed math/rand: %s", err)
|
||||
}
|
||||
|
||||
log.Debugf("seeding math/rand %+v", n.Int64())
|
||||
|
||||
rand.Seed(n.Int64())
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user