#!/usr/bin/ruby

# https://rosettacode.org/wiki/Pseudo-random_numbers/PCG32

class PCG32(seed, incr) {

    has state

    define (
        mask32 = (2**32 - 1),
        mask64 = (2**64 - 1),
        N      = 6364136223846793005,
    )

    method init {
        incr  = (((incr << 1) | 1) & mask64)
        state = (((incr + seed)*N + incr) & mask64)
    }

    method next_int {
        var shift  = ((((state >> 18) ^ state) >> 27) & mask32)
        var rotate = ((state >> 59) & mask32)
            state  = ((state*N + incr) & mask64)
        ((shift >> rotate) | (shift << (32-rotate))) & mask32
    }

    method next_float {
        self.next_int / (mask32+1) -> float
    }
}

say "Seed: 42, Increment: 54, first 5 values:";
var rng = PCG32(seed: 42, incr: 54)
var arr = 5.of { rng.next_int }
assert_eq(arr, [2707161783, 2068313097, 3122475824, 2211639955, 3215226955])
say arr
