mox/mox-/rand.go

47 lines
919 B
Go
Raw Permalink Normal View History

2023-01-30 16:27:06 +03:00
package mox
import (
cryptorand "crypto/rand"
"encoding/binary"
"fmt"
mathrand2 "math/rand/v2"
"sync"
2023-01-30 16:27:06 +03:00
)
type rand struct {
rand *mathrand2.Rand
sync.Mutex
}
// NewPseudoRand returns a new PRNG seeded with random bytes from crypto/rand. Its
// functions can be called concurrently.
func NewPseudoRand() *rand {
var seed [32]byte
if _, err := cryptorand.Read(seed[:]); err != nil {
panic(err)
}
return &rand{rand: mathrand2.New(mathrand2.NewChaCha8(seed))}
}
func (r *rand) Float64() float64 {
r.Lock()
defer r.Unlock()
return r.rand.Float64()
}
func (r *rand) IntN(n int) int {
r.Lock()
defer r.Unlock()
return r.rand.IntN(n)
2023-01-30 16:27:06 +03:00
}
// CryptoRandInt returns a cryptographically random number.
func CryptoRandInt() int64 {
2023-01-30 16:27:06 +03:00
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))
}