;; ported-from: src/games/host/rng.ts @ v1.3.0
;;
;; Seeded RNG for the host sims: mulberry32 — tiny, fast, and good enough for
;; game caprice. Injectable so a gym run is fully reproducible; the live
;; drivers default to Math.random.
;;
;; This is the single most order-sensitive function in the kit: the RL trainer
;; replays whole episodes from a seed, so every arithmetic step below is the
;; exact 32-bit dance of the original (Math.imul, `| 0`, `>>> 0`) and the
;; stream is pinned per seed by test/vectors/rng.json.
(ns ardegazu.peer.games.host.rng)
(defn mulberry32 [seed]
(let [a (volatile! (unsigned-bit-shift-right seed 0))]
(fn []
(vreset! a (bit-or (+ @a 0x6d2b79f5) 0))
(let [av @a
t0 (js/Math.imul (bit-xor av (unsigned-bit-shift-right av 15))
(bit-or 1 av))
t1 (bit-xor (+ t0 (js/Math.imul (bit-xor t0 (unsigned-bit-shift-right t0 7))
(bit-or 61 t0)))
t0)]
(/ (unsigned-bit-shift-right (bit-xor t1 (unsigned-bit-shift-right t1 14)) 0)
4294967296)))))