summaryrefslogtreecommitdiff
path: root/testsuite/tests/lib-random/parallel.ml
blob: 9a8f1c96ae0eac8ac63e2e7971a00fa2caea8056 (plain)
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
(* TEST
 include unix;
 libunix;
 {
   bytecode;
 }{
   native;
 }
*)

let () = Random.init 42

let domain_count = 10

let delays =
  (* These Random calls are intentionally:
   - taken from an independent Random state, not the global state we are testing
   - initialized with make_self_init, to return different delays on each tst run
  *)
  let delay_rng = Random.State.make_self_init () in
  List.init domain_count (fun _i -> Random.State.float delay_rng 0.5)

(* Each domain will start by waiting a random amount, to ensure that
   the Random.int functions we are testing execute in
   non-deterministic order. The Random.int result should remain
   deterministic, as domains are spawned in a deterministic order and
   each domain state is obtaind by splitting the global Random state
   that was initialized with a fixed seed. *)
let f delay () =
  Unix.sleepf delay;
  let a = Random.int 100 in
  let b = Random.int 100 in
  let c = Random.int 100 in
  (a, b, c)

let () =
  delays
  |> List.map (fun delay -> Domain.spawn (f delay))
  |> List.map Domain.join
  |> List.iter (fun (a, b, c) -> Printf.printf "%d %d %d\n%!" a b c)

let () =
  print_endline
    "Note: we observe in this output that the random numbers of each child domain\n\
     appear uncorrelated, yet are produced deterministically."