Created
June 24, 2018 09:26
-
-
Save quillaja/ada8eac82275666368d20397eb3c5be2 to your computer and use it in GitHub Desktop.
PCG psudo random number generator (minimal)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// port of C source: http://www.pcg-random.org/download.html | |
type PCG32 struct { | |
state, seq uint64 | |
} | |
func NewPCG32(stateSeed, seq uint64) *PCG32 { | |
return &PCG32{ | |
state: stateSeed, | |
inc: seq } | |
} | |
func (rng *PCG32) random() uint32 { | |
oldState := rng.state | |
rng.state = oldState*6364136223846793005 + (rng.inc | 1) | |
xorShifted := uint32(((oldState >> 18) ^ oldState) >> 27) | |
rot := uint32(oldState >> 59) | |
return (xorShifted >> rot) | (xorShifted << ((-rot) & 31)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment