Feat[entropy]: Access shared rand.Rand

This commit is contained in:
kayos@tcp.direct 2022-10-11 01:06:20 -07:00
parent a9ce9387ac
commit 3f6dbd7472
Signed by: kayos
GPG Key ID: 4B841471B4BEE979
2 changed files with 13 additions and 0 deletions

View File

@ -39,6 +39,14 @@ func GetOptimizedRand() *rand.Rand {
return rand.New(r)
}
// GetSharedRand returns a pointer to our shared optimized rand.Rand which uses crypto/rand to seed a splitmix64 rng.
func GetSharedRand() *rand.Rand {
getSharedRand.Do(func() {
sharedRand = GetOptimizedRand()
})
return sharedRand
}
// RNGUint32 returns a random uint32 using crypto/rand and splitmix64.
func RNGUint32() uint32 {
getSharedRand.Do(func() {

View File

@ -13,7 +13,12 @@ func check[T comparable](zero T, one T, t *testing.T) {
}
func Test_RNG(t *testing.T) {
// for coverage
sharedRand = GetSharedRand()
RandSleepMS(5)
sharedRand = nil
getSharedRand = &sync.Once{}
// - - - - - -
if OneInA(1000000) {
println(string([]byte{
0x66, 0x75, 0x63, 0x6B, 0x68,