"""mulberry32: a tiny seedable random number generator that web/models.js reproduces bit
for bit, so the random split in the CSV checks is the same in Python and in the browser."""
from __future__ import annotations

M32 = 0xFFFFFFFF


def _imul(a: int, b: int) -> int:
    return (a * b) & M32


class Mulberry32:
    def __init__(self, seed: int):
        self.a = seed & M32

    def random(self) -> float:
        self.a = (self.a + 0x6D2B79F5) & M32
        t = _imul(self.a ^ (self.a >> 15), (1 | self.a) & M32)
        t = ((t + _imul(t ^ (t >> 7), (61 | t) & M32)) & M32) ^ t
        return ((t ^ (t >> 14)) & M32) / 4294967296.0

    def shuffle(self, items: list) -> list:
        """Fisher–Yates, in place, same order of draws as the JS."""
        for i in range(len(items) - 1, 0, -1):
            j = int(self.random() * (i + 1))
            items[i], items[j] = items[j], items[i]
        return items
