runtime: seed fastrand() with hardware randomness

This commit is contained in:
Damian Gryski
2024-09-18 12:28:55 -07:00
committed by Ayke
parent b3e1974c30
commit f3dfe1d49e
2 changed files with 10 additions and 4 deletions
+8 -2
View File
@@ -23,7 +23,13 @@ func fastrand() uint32 {
return xorshift32State
}
var xorshift32State uint32 = 1
func init() {
r, _ := hardwareRand()
xorshift64State = uint64(r | 1) // protect against 0
xorshift32State = uint32(xorshift64State)
}
var xorshift32State uint32
func xorshift32(x uint32) uint32 {
// Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs".
@@ -43,7 +49,7 @@ func fastrand64() uint64 {
return xorshift64State
}
var xorshift64State uint64 = 1
var xorshift64State uint64
// 64-bit xorshift multiply rng from http://vigna.di.unimi.it/ftp/papers/xorshift.pdf
func xorshiftMult64(x uint64) uint64 {