Skip to content

Commit

Permalink
runtime: seed fastrand() with current time
Browse files Browse the repository at this point in the history
  • Loading branch information
dgryski committed Sep 25, 2024
1 parent a26502c commit dabcb16
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/runtime/algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ func fastrand() uint32 {
return xorshift32State
}

var xorshift32State uint32 = 1
func init() {
xorshift64State = uint64(nanotime() | 1) // protect against nanotime() returning 0
xorshift32State = uint32(xorshift64State)
}

var xorshift32State uint32

func xorshift32(x uint32) uint32 {
// Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs".
Expand All @@ -43,7 +48,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 {
Expand Down

0 comments on commit dabcb16

Please sign in to comment.