commit e9bfd1f2edaf55130e7643fa761a25a434ac0bce parent dd8a1806bd968f81aa058f29d5b2bf251a6f89ff Author: NunoSempere <nuno.sempere@protonmail.com> Date: Sat, 14 Oct 2023 19:59:56 +0100 get small ocaml sampling working Diffstat:
| A | ocaml/a.out | | | 0 | |
| A | ocaml/makefile | | | 10 | ++++++++++ |
| A | ocaml/samples.cmi | | | 0 | |
| M | ocaml/samples.ml | | | 16 | +++++++++++++++- |
4 files changed, 25 insertions(+), 1 deletion(-)
diff --git a/ocaml/a.out b/ocaml/a.out Binary files differ. diff --git a/ocaml/makefile b/ocaml/makefile @@ -0,0 +1,10 @@ +# Compiler +CC=ocamlopt +# ocamlopt: platform-specific, faster +# ocamlc: platform-independent intermediate representation, run with ocamlrun + +# Main file +SRC=samples.ml + +build: $(SRC) + $(CC) $(SRC) diff --git a/ocaml/samples.cmi b/ocaml/samples.cmi Binary files differ. diff --git a/ocaml/samples.ml b/ocaml/samples.ml @@ -1 +1,15 @@ -print_endline "Hello world";; +(* Constants *) +let pi = acos (-1.) + +(* Basic samplers *) +let sampleZeroToOne () : float = Random.float 1.0 +let sampleStandardNormal (): float = + let u1 = sampleZeroToOne () in + let u2 = sampleZeroToOne () in + let z = sqrt(-2.0 *. log(u1)) *. sin(2.0 *. pi *. u2) in + z + +let () = + Random.init 1; + Printf.printf "%f\n" (sampleZeroToOne()); + Printf.printf "%f\n" (sampleZeroToOne());