github.com/CAFxX/fastrand@v0.1.0/pcg.go (about) 1 package fastrand 2 3 import ( 4 "math/bits" 5 ) 6 7 // PCG implements the PCG-XSH-RR generator. 8 // This generator is not safe for concurrent use by multiple goroutines. 9 // The zero value is a valid state: Seed() can be called to set a custom seed. 10 type PCG struct { 11 state uint64 12 } 13 14 const ( 15 pcgMult = 6364136223846793005 16 pcgIncr = 1442695040888963407 17 ) 18 19 // Seed initializes the state with the provided seed. 20 // 21 // This function is not safe for concurrent use by multiple goroutines. 22 func (r *PCG) Seed(s uint64) { 23 r.state = s + pcgIncr 24 r.Uint32() // as done by the original C implementation 25 } 26 27 // Uint32 returns a random uint32. 28 // 29 // This function is not safe for concurrent use by multiple goroutines. 30 func (r *PCG) Uint32() uint32 { 31 x := r.state 32 x = x ^ (x >> 18) 33 n := bits.RotateLeft32((uint32)(x>>27), (int)(32-(x>>59))) 34 r.state = x*pcgMult + pcgIncr 35 return n 36 }