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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
import math
import rand
import rand.pcg32
import rand.seed
const (
range_limit = 40
value_count = 1000
seeds = [[u32(42), 242, 267, 14195], [u32(256), 340, 1451, 1505]]
)
const (
sample_size = 1000
stats_epsilon = 0.05
inv_sqrt_12 = 1.0 / math.sqrt(12)
)
fn gen_randoms(seed_data []u32, bound int) []u32 {
mut randoms := []u32{len: 20}
mut rng := pcg32.PCG32RNG{}
rng.seed(seed_data)
for i in 0 .. 20 {
randoms[i] = rng.u32n(u32(bound))
}
return randoms
}
fn test_pcg32_reproducibility() {
seed_data := seed.time_seed_array(4)
randoms1 := gen_randoms(seed_data, 1000)
randoms2 := gen_randoms(seed_data, 1000)
assert randoms1.len == randoms2.len
len := randoms1.len
for i in 0 .. len {
r1 := randoms1[i]
r2 := randoms2[i]
assert r1 == r2
}
}
// TODO: use the `in` syntax and remove this function
// after generics has been completely implemented
fn found(value u64, arr []u64) bool {
for item in arr {
if value == item {
return true
}
}
return false
}
fn test_pcg32_variability() {
// If this test fails and if it is certainly not the implementation
// at fault, try changing the seed values. Repeated values are
// improbable but not impossible.
for seed in seeds {
mut rng := pcg32.PCG32RNG{}
rng.seed(seed)
mut values := []u64{cap: value_count}
for i in 0 .. value_count {
value := rng.u64()
assert !found(value, values)
assert values.len == i
values << value
}
}
}
fn check_uniformity_u64(mut rng pcg32.PCG32RNG, range u64) {
range_f64 := f64(range)
expected_mean := range_f64 / 2.0
mut variance := 0.0
for _ in 0 .. sample_size {
diff := f64(rng.u64n(range)) - expected_mean
variance += diff * diff
}
variance /= sample_size - 1
sigma := math.sqrt(variance)
expected_sigma := range_f64 * inv_sqrt_12
error := (sigma - expected_sigma) / expected_sigma
assert math.abs(error) < stats_epsilon
}
fn test_pcg32_uniformity_u64() {
ranges := [14019545, 80240, 130]
for seed in seeds {
mut rng := pcg32.PCG32RNG{}
rng.seed(seed)
for range in ranges {
check_uniformity_u64(mut rng, u64(range))
}
}
}
fn check_uniformity_f64(mut rng pcg32.PCG32RNG) {
expected_mean := 0.5
mut variance := 0.0
for _ in 0 .. sample_size {
diff := rng.f64() - expected_mean
variance += diff * diff
}
variance /= sample_size - 1
sigma := math.sqrt(variance)
expected_sigma := inv_sqrt_12
error := (sigma - expected_sigma) / expected_sigma
assert math.abs(error) < stats_epsilon
}
fn test_pcg32_uniformity_f64() {
// The f64 version
for seed in seeds {
mut rng := pcg32.PCG32RNG{}
rng.seed(seed)
check_uniformity_f64(mut rng)
}
}
fn test_pcg32_u32n() {
max := u32(16384)
for seed in seeds {
mut rng := pcg32.PCG32RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.u32n(max)
assert value >= 0
assert value < max
}
}
}
fn test_pcg32_u64n() {
max := u64(379091181005)
for seed in seeds {
mut rng := pcg32.PCG32RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.u64n(max)
assert value >= 0
assert value < max
}
}
}
fn test_pcg32_u32_in_range() {
max := u32(484468466)
min := u32(316846)
for seed in seeds {
mut rng := pcg32.PCG32RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.u32_in_range(u32(min), u32(max))
assert value >= min
assert value < max
}
}
}
fn test_pcg32_u64_in_range() {
max := u64(216468454685163)
min := u64(6848646868)
for seed in seeds {
mut rng := pcg32.PCG32RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.u64_in_range(min, max)
assert value >= min
assert value < max
}
}
}
fn test_pcg32_int31() {
max_u31 := int(0x7FFFFFFF)
sign_mask := int(0x80000000)
for seed in seeds {
mut rng := pcg32.PCG32RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.int31()
assert value >= 0
assert value <= max_u31
// This statement ensures that the sign bit is zero
assert (value & sign_mask) == 0
}
}
}
fn test_pcg32_int63() {
max_u63 := i64(0x7FFFFFFFFFFFFFFF)
sign_mask := i64(0x8000000000000000)
for seed in seeds {
mut rng := pcg32.PCG32RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.int63()
assert value >= 0
assert value <= max_u63
assert (value & sign_mask) == 0
}
}
}
fn test_pcg32_intn() {
max := 2525642
for seed in seeds {
mut rng := pcg32.PCG32RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.intn(max)
assert value >= 0
assert value < max
}
}
}
fn test_pcg32_i64n() {
max := i64(3246727724653636)
for seed in seeds {
mut rng := pcg32.PCG32RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.i64n(max)
assert value >= 0
assert value < max
}
}
}
fn test_pcg32_int_in_range() {
min := -4252
max := 1034
for seed in seeds {
mut rng := pcg32.PCG32RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.int_in_range(min, max)
assert value >= min
assert value < max
}
}
}
fn test_pcg32_i64_in_range() {
min := i64(-24095)
max := i64(324058)
for seed in seeds {
mut rng := pcg32.PCG32RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.i64_in_range(min, max)
assert value >= min
assert value < max
}
}
}
fn test_pcg32_f32() {
for seed in seeds {
mut rng := pcg32.PCG32RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.f32()
assert value >= 0.0
assert value < 1.0
}
}
}
fn test_pcg32_f64() {
for seed in seeds {
mut rng := pcg32.PCG32RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.f64()
assert value >= 0.0
assert value < 1.0
}
}
}
fn test_pcg32_f32n() {
max := f32(357.0)
for seed in seeds {
mut rng := pcg32.PCG32RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.f32n(max)
assert value >= 0.0
assert value < max
}
}
}
fn test_pcg32_f64n() {
max := 1.52e6
for seed in seeds {
mut rng := pcg32.PCG32RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.f64n(max)
assert value >= 0.0
assert value < max
}
}
}
fn test_pcg32_f32_in_range() {
min := f32(-24.0)
max := f32(125.0)
for seed in seeds {
mut rng := pcg32.PCG32RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.f32_in_range(min, max)
assert value >= min
assert value < max
}
}
}
fn test_pcg32_f64_in_range() {
min := -548.7
max := 5015.2
for seed in seeds {
mut rng := pcg32.PCG32RNG{}
rng.seed(seed)
for _ in 0 .. range_limit {
value := rng.f64_in_range(min, max)
assert value >= min
assert value < max
}
}
}
fn test_change_default_random_generator() {
rand.set_rng(pcg32.PCG32RNG{})
}
|