3 ms·
If 32 bits are enough, use 32-bit PCG (https://www.pcg-random.org/ https://www.pcg-random.org/) instead In Go: package rand type Src [2]uint64 func Ne
by 37ef_ced3 6y ago
If 32 bits are enough, use 32-bit PCG (https://www.pcg-random.org/ https://www.pcg-random.org/) instead
In Go:
package rand
type Src [2]uint64
func New(bits1, bits2 uint64) *Src {
return &Src{bits1, bits2 | 1}
}
func (s *Src) Uint32() uint32 {
var (
x = s[0]
y = uint32(x >> 59)
z = uint32((x>>18 ^ x) >> 27)
)
s[0] = s[1] + x*6364136223846793005
return z>>y | z<<(-y&31)
}
func (s *Src) LessThan(n uint32) uint32 {
for min := -n % n; ; {
r := s.Uint32()
if r >= min {
return r % n
}
}
}