peer-kit / src / ardegazu / peer / games / host / rng.cljs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
;; 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)))))

static mirror of HEAD · about · clone: git clone https://git.ardegazu.ro/peer-kit.git