"""Random helpers for deterministic seeds."""

from __future__ import annotations

import hashlib

import numpy as np


def deterministic_seed(base_seed: int, *parts: object) -> int:
    """Derive a stable positive 31-bit seed from a base seed + context parts."""
    h = hashlib.blake2b(digest_size=8)
    h.update(str(int(base_seed)).encode("utf-8"))
    for part in parts:
        h.update(b"\x1f")
        h.update(str(part).encode("utf-8"))
    # Keep the seed in the range expected by downstream RNG usage.
    return int(int.from_bytes(h.digest(), "big") % (2**31 - 2) + 1)


def spawn_seeds(seed: int, n: int) -> list[int]:
    rng = np.random.default_rng(seed)
    return [int(x) for x in rng.integers(1, 2**31 - 1, size=n)]
