;;; portal-rng-save.lsp — seed, advance, capture state AND next-5 baseline
;;; to /tmp/cross-rng.sexp so another impl can resume and self-verify.
;;;
;;; Uses S-expression portal (portable across Python, C, asm) rather than
;;; impl-specific JSON/binary format. Proves: (random-state) + (random-state!)
;;; preserves xoshiro256** state across processes AND implementations.

(random-seed! 42)

;; Advance the stream by 5 draws.
(define (advance n)
  (if (= n 0) 'done (begin (random-int 1000000) (advance (- n 1)))))
(advance 5)

;; Snapshot state *before* drawing the 5 baseline values.
(define rng-snapshot (random-state))

;; Compute next 5 draws — these are the baseline the loader must reproduce.
(define baseline
  (list (random-int 1000000) (random-int 1000000) (random-int 1000000)
        (random-int 1000000) (random-int 1000000)))

;; Write a portable portal file. Any impl can (load) it.
(define port (open-output-file "/tmp/cross-rng.sexp"))
(display ";; lumbda cross-impl RNG portal (S-expression)\n" port)
(display "(random-state! '" port) (write rng-snapshot port) (display ")\n" port)
(display "(define expected-baseline '" port) (write baseline port) (display ")\n" port)
(close-port port)

(display "saved baseline: ") (display baseline) (newline)
