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 ;;
|
||||
|
||||
Reference in New Issue
Block a user