"""Version-stable pure-Python randomness primitives for match simulation.

Do not replace these algorithms in-place. Historical simulation/replay depends on
stable seed semantics. Introduce a new primitive/version when randomness rules
must change.
"""

from __future__ import annotations


class SplitMix64:
    """Small deterministic PRNG with stable cross-Python semantics."""

    _MASK = (1 << 64) - 1

    def __init__(self, seed: int):
        if isinstance(seed, bool) or not isinstance(seed, int) or seed < 1:
            raise ValueError("seed must be a positive integer.")
        self._state = seed & self._MASK

    def random(self) -> float:
        self._state = (self._state + 0x9E3779B97F4A7C15) & self._MASK
        value = self._state
        value = ((value ^ (value >> 30)) * 0xBF58476D1CE4E5B9) & self._MASK
        value = ((value ^ (value >> 27)) * 0x94D049BB133111EB) & self._MASK
        value ^= value >> 31
        # Use the high 53 bits so conversion to IEEE-754 double is exact.
        return ((value & self._MASK) >> 11) * (1.0 / (1 << 53))
