Skip to content

Instantly share code, notes, and snippets.

@quillaja
Created June 24, 2018 09:26
Show Gist options
  • Save quillaja/ada8eac82275666368d20397eb3c5be2 to your computer and use it in GitHub Desktop.
Save quillaja/ada8eac82275666368d20397eb3c5be2 to your computer and use it in GitHub Desktop.
PCG psudo random number generator (minimal)
// 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