package neural_nets_lib

  1. Overview
  2. Docs

Source file rand.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
(** Random number generator library with low quality but very reliable reproducibility. *)

module type Random = sig
  val init : int -> unit
  val float_range : float -> float -> float
  val char : unit -> char
  val int : int -> int
end

module Random_for_tests : Random = struct
  let rand = ref (1l : Int32.t)

  let rand_int32 () =
    let open Int32 in
    rand := logxor !rand (shift_left !rand 13);
    rand := logxor !rand (shift_right_logical !rand 17);
    rand := logxor !rand (shift_left !rand 5);
    !rand

  let init seed = rand := Int32.(add (of_int seed) 1l)

  let float_range low high =
    let raw = Int32.(to_float @@ rem (rand_int32 ()) 10000l) in
    (raw /. 10000. *. (high -. low)) +. low

  let char () = Char.chr @@ Int32.(to_int @@ rem (rand_int32 ()) 256l)
  let int high = Int32.(to_int @@ rem (rand_int32 ()) @@ of_int high)
end

module Random_for_dummy_tests : Random = struct
  let rand = ref (1l : Int32.t)

  let rand_int32 () =
    let open Int32 in
    rand := add !rand 1l;
    if equal !rand 10000l then rand := 1l;
    !rand

  let init seed = rand := Int32.(add (of_int seed) 1l)

  let float_range low high =
    let raw = Int32.(to_float @@ rand_int32 ()) in
    (raw /. 10000. *. (high -. low)) +. low

  let char () = Char.chr @@ Int32.(to_int @@ rem (rand_int32 ()) 256l)
  let int high = Int32.(to_int @@ rem (rand_int32 ()) @@ of_int high)
end
OCaml

Innovation. Community. Security.