2 (** {1 Pseudo-random generators}
4 This easiest way to get random numbers (of random values of any type)
5 is to take advantage of the non-determinism of Hoare triples.
6 Simply declaring a function
8 [val random_int: unit -> {} int {}]
10 is typically enough. Two calls to [random_int] return values that can not
11 be proved equal, which is exactly what we need.
13 The following modules provide slightly more: pseudo-random generators
14 which are deterministic according to a state. The state is either
15 explicit (module [State]) or global (module [Random]). Functions [init] allow
16 to reset the generators according to a given seed.
20 (** {2 Mutable states of pseudo-random generators}
22 Basically a reference containing a pure generator. *)
28 type state = private mutable { }
30 val create (seed: int) : state
32 val init (s: state) (seed: int) : unit writes {s}
34 val self_init (s: state) : unit writes {s}
36 val random_bool (s: state) : bool writes {s}
38 val random_int (s: state) (n: int) : int writes {s}
40 ensures { 0 <= result < n }
44 (** {2 A global pseudo-random generator} *)
53 let init (seed: int) = init s seed
55 let self_init () = self_init s
60 let random_int (n: int)
62 ensures { 0 <= result < n }