mirror of
https://github.com/mjl-/mox.git
synced 2025-07-10 07:14:40 +03:00
mox!
This commit is contained in:
22
mox-/rand.go
Normal file
22
mox-/rand.go
Normal file
@ -0,0 +1,22 @@
|
||||
package mox
|
||||
|
||||
import (
|
||||
cryptorand "crypto/rand"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
mathrand "math/rand"
|
||||
)
|
||||
|
||||
// NewRand returns a new PRNG seeded with random bytes from crypto/rand.
|
||||
func NewRand() *mathrand.Rand {
|
||||
return mathrand.New(mathrand.NewSource(cryptoRandInt()))
|
||||
}
|
||||
|
||||
func cryptoRandInt() int64 {
|
||||
buf := make([]byte, 8)
|
||||
_, err := cryptorand.Read(buf)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("reading random bytes: %v", err))
|
||||
}
|
||||
return int64(binary.LittleEndian.Uint64(buf))
|
||||
}
|
Reference in New Issue
Block a user