diff options
author | Damian Gryski <[email protected]> | 2024-09-18 12:28:55 -0700 |
---|---|---|
committer | Ayke <[email protected]> | 2024-10-02 12:40:07 +0200 |
commit | f3dfe1d49ecbcbadc256bea8ba452e1c3b05053a (patch) | |
tree | e0bab102e344b808a61281f6c1528b47d428434b /src | |
parent | b3e1974c304bb5b676bfdb0e193f40658495d61f (diff) | |
download | tinygo-f3dfe1d49ecbcbadc256bea8ba452e1c3b05053a.tar.gz tinygo-f3dfe1d49ecbcbadc256bea8ba452e1c3b05053a.zip |
runtime: seed fastrand() with hardware randomness
Diffstat (limited to 'src')
-rw-r--r-- | src/runtime/algorithm.go | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/src/runtime/algorithm.go b/src/runtime/algorithm.go index 15ca2b7f5..24571498b 100644 --- a/src/runtime/algorithm.go +++ b/src/runtime/algorithm.go @@ -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 { |