Skip to content

The toy generator

The 8-bit permuted congruential generator of Appendix C.

simstreams.pcg8

A toy 8-bit permuted congruential generator (Appendix C).

PCG8 advances an 8-bit linear congruential state and then permutes the old state with an xorshift and a rotation chosen by the state's top bits. It has only 256 states, so its entire cycle can be enumerated, which is what makes it useful for seeing how the full PCG64 generator works.

PCG8

Toy 8-bit PCG generator.

Parameters:

Name Type Description Default
seed int

Initial state, reduced to 8 bits.

0
inc int

Increment of the congruential step. Must be odd.

1
mult int

Multiplier of the congruential step.

141
Source code in src/simstreams/pcg8.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class PCG8:
    """Toy 8-bit PCG generator.

    Parameters
    ----------
    seed : int
        Initial state, reduced to 8 bits.
    inc : int
        Increment of the congruential step. Must be odd.
    mult : int
        Multiplier of the congruential step.
    """

    def __init__(self, seed=0, inc=1, mult=141):
        self.state = seed & 0xFF
        self.inc = inc & 0xFF
        self.mult = mult & 0xFF

        if self.inc % 2 == 0:
            raise ValueError("inc must be odd")

    def random(self):
        """Advance the state and return the next 8-bit output."""
        oldstate = self.state
        self.state = (self.mult * oldstate + self.inc) & 0xFF
        xorshifted = ((oldstate >> 3) ^ oldstate) & 0xFF
        rot = (oldstate >> 5) & 0x7
        return rotr8(xorshifted, rot)

random

random()

Advance the state and return the next 8-bit output.

Source code in src/simstreams/pcg8.py
37
38
39
40
41
42
43
def random(self):
    """Advance the state and return the next 8-bit output."""
    oldstate = self.state
    self.state = (self.mult * oldstate + self.inc) & 0xFF
    xorshifted = ((oldstate >> 3) ^ oldstate) & 0xFF
    rot = (oldstate >> 5) & 0x7
    return rotr8(xorshifted, rot)

rotr8

rotr8(x, r)

Rotate the 8-bit value x right by r bits.

Source code in src/simstreams/pcg8.py
10
11
12
13
def rotr8(x, r):
    """Rotate the 8-bit value ``x`` right by ``r`` bits."""
    r %= 8
    return ((x >> r) | (x << (8 - r))) & 0xFF