ocaml: phase 6 Random module (deterministic LCG) (+4 tests, 549 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 48s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 48s
Linear-congruential PRNG with mutable seed (_state ref). API: init s seed the PRNG self_init () default seed (1) int bound 0 <= n < bound bool () fair coin float bound uniform in [0, bound) bits () 30 bits Stepping rule: state := (state * 1103515245 + 12345) mod 2147483647 result := |state| mod bound Same seed reproduces the same sequence. Real OCaml's Random uses Lagged Fibonacci; ours is simpler but adequate for shuffles and Monte Carlo demos in baseline programs. Random.init 42; Random.int 100 = 48 Random.init 1; Random.int 10 = 0
This commit is contained in:
@@ -609,6 +609,30 @@
|
||||
let asprintf fmt = Printf.sprintf fmt
|
||||
end ;;
|
||||
|
||||
module Random = struct
|
||||
(* Linear-congruential PRNG. Deterministic for testing — same
|
||||
seed reproduces the same sequence. Real OCaml's Random uses
|
||||
Lagged Fibonacci; ours is simpler but adequate for shuffles
|
||||
and Monte Carlo demos in baseline programs. *)
|
||||
let _state = ref 1
|
||||
|
||||
let init s = _state := s
|
||||
|
||||
let self_init () = _state := 1
|
||||
|
||||
let int bound =
|
||||
_state := (!_state * 1103515245 + 12345) mod 2147483647;
|
||||
Int.abs (!_state) mod bound
|
||||
|
||||
let bool () = int 2 = 1
|
||||
|
||||
let float bound =
|
||||
let n = int 1000000 in
|
||||
float_of_int n /. 1000000.0 *. bound
|
||||
|
||||
let bits () = int 1073741824
|
||||
end ;;
|
||||
|
||||
module Lazy = struct
|
||||
let force lz = _lazy_force lz
|
||||
end ;;
|
||||
|
||||
@@ -1360,6 +1360,16 @@ cat > "$TMPFILE" << 'EPOCHS'
|
||||
(epoch 5103)
|
||||
(eval "(ocaml-run \"let t = Hashtbl.create 4 in Hashtbl.add t \\\"a\\\" 1; Hashtbl.clear t; Hashtbl.length t\")")
|
||||
|
||||
;; ── Random module (deterministic LCG) ─────────────────────────
|
||||
(epoch 5110)
|
||||
(eval "(ocaml-run \"Random.init 42; Random.int 100\")")
|
||||
(epoch 5111)
|
||||
(eval "(ocaml-run \"Random.init 42; let a = Random.int 100 in let b = Random.int 100 in let c = Random.int 100 in a + b + c\")")
|
||||
(epoch 5112)
|
||||
(eval "(ocaml-run \"Random.init 1; Random.int 10\")")
|
||||
(epoch 5113)
|
||||
(eval "(ocaml-run \"Random.init 7; Random.bool ()\")")
|
||||
|
||||
EPOCHS
|
||||
|
||||
OUTPUT=$(timeout 360 "$SX_SERVER" < "$TMPFILE" 2>/dev/null)
|
||||
@@ -2161,6 +2171,12 @@ check 5101 "Hashtbl.values sum" '12'
|
||||
check 5102 "Hashtbl.remove + length" '1'
|
||||
check 5103 "Hashtbl.clear + length" '0'
|
||||
|
||||
# ── Random (deterministic LCG) ──────────────────────────────────
|
||||
check 5110 "Random.int 100 seed=42" '48'
|
||||
check 5111 "Random.int x3 seed=42 sum" '152'
|
||||
check 5112 "Random.int 10 seed=1" '0'
|
||||
check 5113 "Random.bool seed=7" 'true'
|
||||
|
||||
TOTAL=$((PASS + FAIL))
|
||||
if [ $FAIL -eq 0 ]; then
|
||||
echo "ok $PASS/$TOTAL OCaml-on-SX tests passed"
|
||||
|
||||
Reference in New Issue
Block a user