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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
open! Import
module Int = Int0
module Char = Char0
let forbid_nondeterminism_in_tests ~allow_in_tests =
if am_testing
then (
match allow_in_tests with
| Some true -> ()
| None | Some false ->
failwith
"initializing Random with a nondeterministic seed is forbidden in inline tests")
;;
external random_seed : unit -> int array = "caml_sys_random_seed"
let random_seed ?allow_in_tests () =
forbid_nondeterminism_in_tests ~allow_in_tests;
random_seed ()
;;
module State = struct
type t = Caml.Random.State.t Lazy.t
let bits t = Caml.Random.State.bits (Lazy.force t)
let bool t = Caml.Random.State.bool (Lazy.force t)
let int t x = Caml.Random.State.int (Lazy.force t) x
let int32 t x = Caml.Random.State.int32 (Lazy.force t) x
let int64 t x = Caml.Random.State.int64 (Lazy.force t) x
let nativeint t x = Caml.Random.State.nativeint (Lazy.force t) x
let make seed = Lazy.from_val (Caml.Random.State.make seed)
let copy t = Lazy.from_val (Caml.Random.State.copy (Lazy.force t))
let char t = int t 256 |> Char.unsafe_of_int
let ascii t = int t 128 |> Char.unsafe_of_int
let make_self_init ?allow_in_tests () =
forbid_nondeterminism_in_tests ~allow_in_tests;
Lazy.from_val (Caml.Random.State.make_self_init ())
;;
let assign = Random_repr.assign
let full_init t seed = assign t (make seed)
let default =
if am_testing
then (
let t = Caml.Random.get_state () in
Caml.Random.init 137;
Lazy.from_val t)
else
lazy
(Lazy.force (make_self_init ()))
;;
let int_on_64bits t bound =
if bound <= 0x3FFFFFFF
then int t bound
else Caml.Int64.to_int (int64 t (Caml.Int64.of_int bound))
;;
let int_on_32bits t bound =
if bound <= 0x3FFFFFFF
then int t bound
else Caml.Int32.to_int (int32 t (Caml.Int32.of_int bound))
;;
let int =
match Word_size.word_size with
| W64 -> int_on_64bits
| W32 -> int_on_32bits
;;
let full_range_int64 =
let open Caml.Int64 in
let bits state = of_int (bits state) in
fun state ->
logxor
(bits state)
(logxor (shift_left (bits state) 30) (shift_left (bits state) 60))
;;
let full_range_int32 =
let open Caml.Int32 in
let bits state = of_int (bits state) in
fun state -> logxor (bits state) (shift_left (bits state) 30)
;;
let full_range_int_on_64bits state = Caml.Int64.to_int (full_range_int64 state)
let full_range_int_on_32bits state = Caml.Int32.to_int (full_range_int32 state)
let full_range_int =
match Word_size.word_size with
| W64 -> full_range_int_on_64bits
| W32 -> full_range_int_on_32bits
;;
let full_range_nativeint_on_64bits state =
Caml.Int64.to_nativeint (full_range_int64 state)
;;
let full_range_nativeint_on_32bits state =
Caml.Nativeint.of_int32 (full_range_int32 state)
;;
let full_range_nativeint =
match Word_size.word_size with
| W64 -> full_range_nativeint_on_64bits
| W32 -> full_range_nativeint_on_32bits
;;
let raise_crossed_bounds name lower_bound upper_bound string_of_bound =
Printf.failwithf
"Random.%s: crossed bounds [%s > %s]"
name
(string_of_bound lower_bound)
(string_of_bound upper_bound)
()
[@@cold] [@@inline never] [@@local never] [@@specialise never]
;;
let int_incl =
let rec in_range state lo hi =
let int = full_range_int state in
if int >= lo && int <= hi then int else in_range state lo hi
in
fun state lo hi ->
if lo > hi then raise_crossed_bounds "int" lo hi Int.to_string;
let diff = hi - lo in
if diff = Int.max_value
then lo + (full_range_int state land Int.max_value)
else if diff >= 0
then lo + int state (Int.succ diff)
else in_range state lo hi
;;
let int32_incl =
let open Int32_replace_polymorphic_compare in
let rec in_range state lo hi =
let int = full_range_int32 state in
if int >= lo && int <= hi then int else in_range state lo hi
in
let open Caml.Int32 in
fun state lo hi ->
if lo > hi then raise_crossed_bounds "int32" lo hi to_string;
let diff = sub hi lo in
if diff = max_int
then add lo (logand (full_range_int32 state) max_int)
else if diff >= 0l
then add lo (int32 state (succ diff))
else in_range state lo hi
;;
let nativeint_incl =
let open Nativeint_replace_polymorphic_compare in
let rec in_range state lo hi =
let int = full_range_nativeint state in
if int >= lo && int <= hi then int else in_range state lo hi
in
let open Caml.Nativeint in
fun state lo hi ->
if lo > hi then raise_crossed_bounds "nativeint" lo hi to_string;
let diff = sub hi lo in
if diff = max_int
then add lo (logand (full_range_nativeint state) max_int)
else if diff >= 0n
then add lo (nativeint state (succ diff))
else in_range state lo hi
;;
let int64_incl =
let open Int64_replace_polymorphic_compare in
let rec in_range state lo hi =
let int = full_range_int64 state in
if int >= lo && int <= hi then int else in_range state lo hi
in
let open Caml.Int64 in
fun state lo hi ->
if lo > hi then raise_crossed_bounds "int64" lo hi to_string;
let diff = sub hi lo in
if diff = max_int
then add lo (logand (full_range_int64 state) max_int)
else if diff >= 0L
then add lo (int64 state (succ diff))
else in_range state lo hi
;;
let rec rawfloat state =
let open Float_replace_polymorphic_compare in
let scale = 0x1p-30 in
let r1 = Caml.float_of_int (bits state) in
let r2 = Caml.float_of_int (bits state) in
let result = ((r1 *. scale) +. r2) *. scale in
if result < 1.0 then result else rawfloat state
;;
let float state hi = rawfloat state *. hi
let float_range state lo hi =
let open Float_replace_polymorphic_compare in
if lo > hi then raise_crossed_bounds "float" lo hi Caml.string_of_float;
lo +. float state (hi -. lo)
;;
end
let default = Random_repr.make_default State.default
let bits () = State.bits (Random_repr.get_state default)
let int x = State.int (Random_repr.get_state default) x
let int32 x = State.int32 (Random_repr.get_state default) x
let nativeint x = State.nativeint (Random_repr.get_state default) x
let int64 x = State.int64 (Random_repr.get_state default) x
let float x = State.float (Random_repr.get_state default) x
let int_incl x y = State.int_incl (Random_repr.get_state default) x y
let int32_incl x y = State.int32_incl (Random_repr.get_state default) x y
let nativeint_incl x y = State.nativeint_incl (Random_repr.get_state default) x y
let int64_incl x y = State.int64_incl (Random_repr.get_state default) x y
let float_range x y = State.float_range (Random_repr.get_state default) x y
let bool () = State.bool (Random_repr.get_state default)
let char () = State.char (Random_repr.get_state default)
let ascii () = State.ascii (Random_repr.get_state default)
let full_init seed = State.full_init (Random_repr.get_state default) seed
let init seed = full_init [| seed |]
let self_init ?allow_in_tests () = full_init (random_seed ?allow_in_tests ())
let set_state s = State.assign (Random_repr.get_state default) s