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
external random_seed: unit -> int array = "caml_sys_random_seed"
module State = struct
open Bigarray
type t = (int64, int64_elt, c_layout) Array1.t
external next: t -> (int64[@unboxed])
= "caml_lxm_next" "caml_lxm_next_unboxed" [@@noalloc]
let create () : t =
Array1.create Int64 C_layout 4
let set s i1 i2 i3 i4 =
Array1.unsafe_set s 0 (Int64.logor i1 1L);
Array1.unsafe_set s 1 i2;
Array1.unsafe_set s 2 (if i3 <> 0L then i3 else 1L);
Array1.unsafe_set s 3 (if i4 <> 0L then i4 else 2L)
let mk i1 i2 i3 i4 =
let s = create () in
set s i1 i2 i3 i4; s
let serialization_prefix =
"lxm1:"
let serialization_prefix_len =
String.length serialization_prefix
let to_binary_string s =
let prefix = serialization_prefix in
let preflen = serialization_prefix_len in
let buf = Bytes.create (preflen + 4 * 8) in
Bytes.blit_string prefix 0 buf 0 preflen;
for i = 0 to 3 do
Bytes.set_int64_le buf (preflen + i * 8) (Array1.get s i)
done;
Bytes.unsafe_to_string buf
let of_binary_string buf =
let prefix = serialization_prefix in
let preflen = serialization_prefix_len in
if String.length buf <> preflen + 4 * 8
|| not (String.starts_with ~prefix buf)
then
failwith
("Random.State.of_binary_string: expected a format \
compatible with OCaml " ^ Sys.ocaml_version);
let i1 = String.get_int64_le buf (preflen + 0 * 8) in
let i2 = String.get_int64_le buf (preflen + 1 * 8) in
let i3 = String.get_int64_le buf (preflen + 2 * 8) in
let i4 = String.get_int64_le buf (preflen + 3 * 8) in
mk i1 i2 i3 i4
let assign (dst: t) (src: t) =
Array1.blit src dst
let copy s =
let s' = create() in assign s' s; s'
let reinit s seed =
let n = Array.length seed in
let b = Bytes.create (n * 8 + 1) in
for i = 0 to n-1 do
Bytes.set_int64_le b (i * 8) (Int64.of_int seed.(i))
done;
Bytes.set b (n * 8) '\x01';
let d1 = Digest.bytes b in
Bytes.set b (n * 8) '\x02';
let d2 = Digest.bytes b in
set s (String.get_int64_le d1 0)
(String.get_int64_le d1 8)
(String.get_int64_le d2 0)
(String.get_int64_le d2 8)
let make seed =
let s = create() in reinit s seed; s
let make_self_init () =
make (random_seed ())
let bits s =
Int64.to_int (next s) land 0x3FFF_FFFF
let rec intaux s n =
let r = bits s in
let v = r mod n in
if r - v > 0x3FFFFFFF - n + 1 then intaux s n else v
let int s bound =
if bound > 0x3FFFFFFF || bound <= 0
then invalid_arg "Random.int"
else intaux s bound
let rec int63aux s n =
let r = Int64.to_int (next s) land max_int in
let v = r mod n in
if r - v > max_int - n + 1 then int63aux s n else v
let full_int s bound =
if bound <= 0 then
invalid_arg "Random.full_int"
else if bound > 0x3FFFFFFF then
int63aux s bound
else
intaux s bound
let bits32 s =
Int64.to_int32 (next s)
let rec int32aux s n =
let r = Int32.shift_right_logical (bits32 s) 1 in
let v = Int32.rem r n in
if Int32.(sub r v > add (sub max_int n) 1l)
then int32aux s n
else v
let int32 s bound =
if bound <= 0l
then invalid_arg "Random.int32"
else int32aux s bound
let bits64 s =
next s
let rec int64aux s n =
let r = Int64.shift_right_logical (bits64 s) 1 in
let v = Int64.rem r n in
if Int64.(sub r v > add (sub max_int n) 1L)
then int64aux s n
else v
let int64 s bound =
if bound <= 0L
then invalid_arg "Random.int64"
else int64aux s bound
let nativebits =
if Nativeint.size = 32
then fun s -> Nativeint.of_int32 (bits32 s)
else fun s -> Int64.to_nativeint (bits64 s)
let nativeint =
if Nativeint.size = 32
then fun s bound -> Nativeint.of_int32 (int32 s (Nativeint.to_int32 bound))
else fun s bound -> Int64.to_nativeint (int64 s (Int64.of_nativeint bound))
let rec rawfloat s =
let b = next s in
let n = Int64.shift_right_logical b 11 in
if n <> 0L then Int64.to_float n *. 0x1.p-53 else rawfloat s
let float s bound = rawfloat s *. bound
let bool s = next s < 0L
let split s =
let i1 = bits64 s in let i2 = bits64 s in
let i3 = bits64 s in let i4 = bits64 s in
mk i1 i2 i3 i4
end
let mk_default () =
State.mk (-6196874289567705097L)
586573249833713189L
(-8591268803865043407L)
6388613595849772044L
let random_key =
Domain.DLS.new_key ~split_from_parent:State.split mk_default
let bits () = State.bits (Domain.DLS.get random_key)
let int bound = State.int (Domain.DLS.get random_key) bound
let full_int bound = State.full_int (Domain.DLS.get random_key) bound
let int32 bound = State.int32 (Domain.DLS.get random_key) bound
let nativeint bound = State.nativeint (Domain.DLS.get random_key) bound
let int64 bound = State.int64 (Domain.DLS.get random_key) bound
let float scale = State.float (Domain.DLS.get random_key) scale
let bool () = State.bool (Domain.DLS.get random_key)
let bits32 () = State.bits32 (Domain.DLS.get random_key)
let bits64 () = State.bits64 (Domain.DLS.get random_key)
let nativebits () = State.nativebits (Domain.DLS.get random_key)
let full_init seed = State.reinit (Domain.DLS.get random_key) seed
let init seed = full_init [| seed |]
let self_init () = full_init (random_seed())
let split () = State.split (Domain.DLS.get random_key)
let get_state () = State.copy (Domain.DLS.get random_key)
let set_state s = State.assign (Domain.DLS.get random_key) s