sylva-rs 2.0.0

Terrestrial and airborne laser scanning processing for forest ecology and remote sensing research.
Documentation
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// Sylva: LiDAR processing for forest ecology and remote sensing research.
// Copyright (C) 2026 Tim Devereux, The University of Queensland.
// Free software under the GNU General Public License v3.0 or later;
// see the LICENSE file. There is no warranty, to the extent permitted by law.
//! NumPy's `Generator` distributions on [`Generator`], reproduced to the bit.
//!
//! `uniform(low, high)` is `low + (high - low) * random()`; `integers` draws
//! by Lemire's method as `bounded` does; `standard_normal` is NumPy's
//! 256-layer ziggurat (Marsaglia and Tsang 2000, with the exponential tail
//! sampler and the `log1p(-u)` form NumPy uses), on NumPy's own tables, and
//! `standard_exponential` its 256-layer exponential ziggurat. A seeded
//! computation that moved from NumPy into the core therefore draws the same
//! numbers the NumPy code drew.

use crate::util::nprandom::Generator;

const ZIGGURAT_NOR_R: f64 = 3.654152885361009;
const ZIGGURAT_NOR_INV_R: f64 = 0.2736612373297583;
const ZIGGURAT_EXP_R: f64 = 7.6971174701310497140446280481;

impl Generator {
    /// `Generator.uniform(low, high)`: one draw from `[low, high)`.
    pub fn uniform(&mut self, low: f64, high: f64) -> f64 {
        let range = high - low;
        low + range * self.random()
    }

    /// `n` draws of [`Generator::uniform`], as `uniform(low, high, n)`.
    pub fn uniform_n(&mut self, low: f64, high: f64, n: usize) -> Vec<f64> {
        (0..n).map(|_| self.uniform(low, high)).collect()
    }

    /// `Generator.standard_normal()`: NumPy's ziggurat.
    pub fn standard_normal(&mut self) -> f64 {
        loop {
            let mut r = self.next_u64();
            let idx = (r & 0xff) as usize;
            r >>= 8;
            let sign = r & 0x1;
            let rabs = (r >> 1) & 0x000f_ffff_ffff_ffff;
            let mut x = rabs as f64 * WI[idx];
            if sign & 0x1 == 1 {
                x = -x;
            }
            if rabs < KI[idx] {
                return x;
            }
            if idx == 0 {
                loop {
                    let xx = -ZIGGURAT_NOR_INV_R * (-self.random()).ln_1p();
                    let yy = -(-self.random()).ln_1p();
                    if yy + yy > xx * xx {
                        return if (rabs >> 8) & 0x1 == 1 { -(ZIGGURAT_NOR_R + xx) } else { ZIGGURAT_NOR_R + xx };
                    }
                }
            } else if (FI[idx - 1] - FI[idx]) * self.random() + FI[idx] < (-0.5 * x * x).exp() {
                return x;
            }
        }
    }

    /// `Generator.normal(loc, scale)`: `loc + scale * standard_normal()`.
    pub fn normal(&mut self, loc: f64, scale: f64) -> f64 {
        loc + scale * self.standard_normal()
    }

    /// `n` draws of [`Generator::normal`], as `normal(loc, scale, n)`.
    pub fn normal_n(&mut self, loc: f64, scale: f64, n: usize) -> Vec<f64> {
        (0..n).map(|_| self.normal(loc, scale)).collect()
    }

    /// `Generator.integers(low, high)` (int64, `high` excluded). Panics if
    /// `high <= low`.
    pub fn integers(&mut self, low: i64, high: i64) -> i64 {
        assert!(high > low, "high <= low");
        let range = (high as u64).wrapping_sub(low as u64).wrapping_sub(1);
        (low as u64).wrapping_add(self.bounded(range)) as i64
    }
}

// NumPy's ziggurat tables (numpy/random/src/distributions/ziggurat_constants.h).
const KI: [u64; 256] = [
    0x0EF33D8025EF6A, 0x00000000000000, 0x0C08BE98FBC6A8, 0x0DA354FABD8142, 0x0E51F67EC1EEEA, 0x0EB255E9D3F77E,
    0x0EEF4B817ECAB9, 0x0F19470AFA44AA, 0x0F37ED61FFCB18, 0x0F4F469561255C, 0x0F61A5E41BA396, 0x0F707A755396A4,
    0x0F7CB2EC28449A, 0x0F86F10C6357D3, 0x0F8FA6578325DE, 0x0F9724C74DD0DA, 0x0F9DA907DBF509, 0x0FA360F581FA74,
    0x0FA86FDE5B4BF8, 0x0FACF160D354DC, 0x0FB0FB6718B90F, 0x0FB49F8D5374C6, 0x0FB7EC2366FE77, 0x0FBAECE9A1E50E,
    0x0FBDAB9D040BED, 0x0FC03060FF6C57, 0x0FC2821037A248, 0x0FC4A67AE25BD1, 0x0FC6A2977AEE31, 0x0FC87AA92896A4,
    0x0FCA325E4BDE85, 0x0FCBCCE902231A, 0x0FCD4D12F839C4, 0x0FCEB54D8FEC99, 0x0FD007BF1DC930, 0x0FD1464DD6C4E6,
    0x0FD272A8E2F450, 0x0FD38E4FF0C91E, 0x0FD49A9990B478, 0x0FD598B8920F53, 0x0FD689C08E99EC, 0x0FD76EA9C8E832,
    0x0FD848547B08E8, 0x0FD9178BAD2C8C, 0x0FD9DD07A7ADD2, 0x0FDA9970105E8C, 0x0FDB4D5DC02E20, 0x0FDBF95C5BFCD0,
    0x0FDC9DEBB99A7D, 0x0FDD3B8118729D, 0x0FDDD288342F90, 0x0FDE6364369F64, 0x0FDEEE708D514E, 0x0FDF7401A6B42E,
    0x0FDFF46599ED40, 0x0FE06FE4BC24F2, 0x0FE0E6C225A258, 0x0FE1593C28B84C, 0x0FE1C78CBC3F99, 0x0FE231E9DB1CAA,
    0x0FE29885DA1B91, 0x0FE2FB8FB54186, 0x0FE35B33558D4A, 0x0FE3B799D0002A, 0x0FE410E99EAD7F, 0x0FE46746D47734,
    0x0FE4BAD34C095C, 0x0FE50BAED29524, 0x0FE559F74EBC78, 0x0FE5A5C8E41212, 0x0FE5EF3E138689, 0x0FE6366FD91078,
    0x0FE67B75C6D578, 0x0FE6BE661E11AA, 0x0FE6FF55E5F4F2, 0x0FE73E5900A702, 0x0FE77B823E9E39, 0x0FE7B6E37070A2,
    0x0FE7F08D774243, 0x0FE8289053F08C, 0x0FE85EFB35173A, 0x0FE893DC840864, 0x0FE8C741F0CEBC, 0x0FE8F9387D4EF6,
    0x0FE929CC879B1D, 0x0FE95909D388EA, 0x0FE986FB939AA2, 0x0FE9B3AC714866, 0x0FE9DF2694B6D5, 0x0FEA0973ABE67C,
    0x0FEA329CF166A4, 0x0FEA5AAB32952C, 0x0FEA81A6D5741A, 0x0FEAA797DE1CF0, 0x0FEACC85F3D920, 0x0FEAF07865E63C,
    0x0FEB13762FEC13, 0x0FEB3585FE2A4A, 0x0FEB56AE3162B4, 0x0FEB76F4E284FA, 0x0FEB965FE62014, 0x0FEBB4F4CF9D7C,
    0x0FEBD2B8F449D0, 0x0FEBEFB16E2E3E, 0x0FEC0BE31EBDE8, 0x0FEC2752B15A15, 0x0FEC42049DAFD3, 0x0FEC5BFD29F196,
    0x0FEC75406CEEF4, 0x0FEC8DD2500CB4, 0x0FECA5B6911F12, 0x0FECBCF0C427FE, 0x0FECD38454FB15, 0x0FECE97488C8B3,
    0x0FECFEC47F91B7, 0x0FED1377358528, 0x0FED278F844903, 0x0FED3B10242F4C, 0x0FED4DFBAD586E, 0x0FED605498C3DD,
    0x0FED721D414FE8, 0x0FED8357E4A982, 0x0FED9406A42CC8, 0x0FEDA42B85B704, 0x0FEDB3C8746AB4, 0x0FEDC2DF416652,
    0x0FEDD171A46E52, 0x0FEDDF813C8AD3, 0x0FEDED0F909980, 0x0FEDFA1E0FD414, 0x0FEE06AE124BC4, 0x0FEE12C0D95A06,
    0x0FEE1E579006E0, 0x0FEE29734B6524, 0x0FEE34150AE4BC, 0x0FEE3E3DB89B3C, 0x0FEE47EE2982F4, 0x0FEE51271DB086,
    0x0FEE59E9407F41, 0x0FEE623528B42E, 0x0FEE6A0B5897F1, 0x0FEE716C3E077A, 0x0FEE7858327B82, 0x0FEE7ECF7B06BA,
    0x0FEE84D2484AB2, 0x0FEE8A60B66343, 0x0FEE8F7ACCC851, 0x0FEE94207E25DA, 0x0FEE9851A829EA, 0x0FEE9C0E13485C,
    0x0FEE9F557273F4, 0x0FEEA22762CCAE, 0x0FEEA4836B42AC, 0x0FEEA668FC2D71, 0x0FEEA7D76ED6FA, 0x0FEEA8CE04FA0A,
    0x0FEEA94BE8333B, 0x0FEEA950296410, 0x0FEEA8D9C0075E, 0x0FEEA7E7897654, 0x0FEEA678481D24, 0x0FEEA48AA29E83,
    0x0FEEA21D22E4DA, 0x0FEE9F2E352024, 0x0FEE9BBC26AF2E, 0x0FEE97C524F2E4, 0x0FEE93473C0A3A, 0x0FEE8E40557516,
    0x0FEE88AE369C7A, 0x0FEE828E7F3DFD, 0x0FEE7BDEA7B888, 0x0FEE749BFF37FF, 0x0FEE6CC3A9BD5E, 0x0FEE64529E007E,
    0x0FEE5B45A32888, 0x0FEE51994E57B6, 0x0FEE474A0006CF, 0x0FEE3C53E12C50, 0x0FEE30B2E02AD8, 0x0FEE2462AD8205,
    0x0FEE175EB83C5A, 0x0FEE09A22A1447, 0x0FEDFB27E349CC, 0x0FEDEBEA76216C, 0x0FEDDBE422047E, 0x0FEDCB0ECE39D3,
    0x0FEDB964042CF4, 0x0FEDA6DCE938C9, 0x0FED937237E98D, 0x0FED7F1C38A836, 0x0FED69D2B9C02B, 0x0FED538D06AE00,
    0x0FED3C41DEA422, 0x0FED23E76A2FD8, 0x0FED0A732FE644, 0x0FECEFDA07FE34, 0x0FECD4100EB7B8, 0x0FECB708956EB4,
    0x0FEC98B61230C1, 0x0FEC790A0DA978, 0x0FEC57F50F31FE, 0x0FEC356686C962, 0x0FEC114CB4B335, 0x0FEBEB948E6FD0,
    0x0FEBC429A0B692, 0x0FEB9AF5EE0CDC, 0x0FEB6FE1C98542, 0x0FEB42D3AD1F9E, 0x0FEB13B00B2D4B, 0x0FEAE2591A02E9,
    0x0FEAAEAE992257, 0x0FEA788D8EE326, 0x0FEA3FCFFD73E5, 0x0FEA044C8DD9F6, 0x0FE9C5D62F563B, 0x0FE9843BA947A4,
    0x0FE93F471D4728, 0x0FE8F6BD76C5D6, 0x0FE8AA5DC4E8E6, 0x0FE859E07AB1EA, 0x0FE804F690A940, 0x0FE7AB488233C0,
    0x0FE74C751F6AA5, 0x0FE6E8102AA202, 0x0FE67DA0B6ABD8, 0x0FE60C9F38307E, 0x0FE5947338F742, 0x0FE51470977280,
    0x0FE48BD436F458, 0x0FE3F9BFFD1E37, 0x0FE35D35EEB19C, 0x0FE2B5122FE4FE, 0x0FE20003995557, 0x0FE13C82788314,
    0x0FE068C4EE67B0, 0x0FDF82B02B71AA, 0x0FDE87C57EFEAA, 0x0FDD7509C63BFD, 0x0FDC46E529BF13, 0x0FDAF8F82E0282,
    0x0FD985E1B2BA75, 0x0FD7E6EF48CF04, 0x0FD613ADBD650B, 0x0FD40149E2F012, 0x0FD1A1A7B4C7AC, 0x0FCEE204761F9E,
    0x0FCBA8D85E11B2, 0x0FC7D26ECD2D22, 0x0FC32B2F1E22ED, 0x0FBD6581C0B83A, 0x0FB606C4005434, 0x0FAC40582A2874,
    0x0F9E971E014598, 0x0F89FA48A41DFC, 0x0F66C5F7F0302C, 0x0F1A5A4B331C4A,
];

const WI: [f64; 256] = [
    8.683627060801306e-16, 4.779330175727737e-17, 6.354352417405262e-17, 7.454870481247696e-17,
    8.3293668157931e-17, 9.068060405059482e-17, 9.714860076567762e-17, 1.0294750314241019e-16,
    1.0823430288447684e-16, 1.131147019610903e-16, 1.176635945702292e-16, 1.2193617278714363e-16,
    1.2597439914637093e-16, 1.2981099886264032e-16, 1.3347203736824123e-16, 1.3697864842571203e-16,
    1.4034823001242382e-16, 1.4359529452056943e-16, 1.4673208742364422e-16, 1.4976904668391037e-16,
    1.5271515003596198e-16, 1.5557818169460764e-16, 1.5836494009290885e-16, 1.6108140175274928e-16,
    1.6373285203969853e-16, 1.6632399058420835e-16, 1.6885901708676596e-16, 1.713417017655966e-16,
    1.737754436586486e-16, 1.7616331923000996e-16, 1.7850812316976727e-16, 1.8081240285799152e-16,
    1.830784876482675e-16, 1.853085138861802e-16, 1.8750444639373882e-16, 1.896680970077476e-16,
    1.918011406483862e-16, 1.9390512930625104e-16, 1.9598150426628824e-16, 1.9803160683128174e-16,
    2.000566877627333e-16, 2.0205791562071654e-16, 2.0403638415480212e-16, 2.0599311887403706e-16,
    2.079290829041402e-16, 2.0984518222370352e-16, 2.1174227035760342e-16, 2.1362115259449868e-16,
    2.1548258978581458e-16, 2.1732730177564367e-16, 2.191559705042727e-16, 2.2096924282235318e-16,
    2.2276773304789553e-16, 2.2455202529414355e-16, 2.263226755928568e-16, 2.280802138345017e-16,
    2.2982514554424684e-16, 2.3155795351040804e-16, 2.3327909928004356e-16, 2.3498902453470955e-16,
    2.3668815235791604e-16, 2.3837688840454243e-16, 2.4005562198135063e-16, 2.4172472704675025e-16,
    2.433845631371103e-16, 2.4503547622614954e-16, 2.466777995232705e-16, 2.4831185421610877e-16,
    2.4993795016204524e-16, 2.515563865329658e-16, 2.5316745241713583e-16, 2.547714273816944e-16,
    2.563685819989397e-16, 2.579591783392867e-16, 2.5954347043351707e-16, 2.6112170470670194e-16,
    2.6269412038597256e-16, 2.6426094988411895e-16, 2.658224191608307e-16, 2.6737874806323633e-16,
    2.689301506472616e-16, 2.704768354811995e-16, 2.720190059327732e-16, 2.735568604408679e-16,
    2.7509059277301666e-16, 2.7662039226963903e-16, 2.781464440759544e-16, 2.79668929362423e-16,
    2.8118802553450207e-16, 2.827039064324479e-16, 2.842167425218406e-16, 2.8572670107546015e-16,
    2.87233946347098e-16, 2.887386397378482e-16, 2.9024093995538423e-16, 2.9174100316669455e-16,
    2.9323898314471816e-16, 2.947350314092935e-16, 2.9622929736280665e-16, 2.977219284209029e-16,
    2.992130701386013e-16, 3.007028663321331e-16, 3.0219145919680615e-16, 3.036789894211802e-16,
    3.051655962978219e-16, 3.0665141783089545e-16, 3.081365908408297e-16, 3.0962125106629225e-16,
    3.111055332636893e-16, 3.125895713043999e-16, 3.140734982699446e-16, 3.1555744654528006e-16,
    3.1704154791040285e-16, 3.1852593363044065e-16, 3.2001073454440114e-16, 3.214960811527447e-16,
    3.2298210370394156e-16, 3.244689322801698e-16, 3.2595669688230784e-16, 3.2744552751437067e-16,
    3.2893555426753697e-16, 3.3042690740391284e-16, 3.3191971744017523e-16, 3.3341411523123725e-16,
    3.3491023205407785e-16, 3.364081996918765e-16, 3.37908150518595e-16, 3.394102175841489e-16,
    3.409145347003126e-16, 3.424212365275018e-16, 3.4393045866258313e-16, 3.454423377278584e-16,
    3.4695701146137835e-16, 3.4847461880874137e-16, 3.499953000165381e-16, 3.5151919672760744e-16,
    3.53046452078274e-16, 3.5457721079774357e-16, 3.5611161930983884e-16, 3.5764982583726505e-16,
    3.59191980508603e-16, 3.6073823546823514e-16, 3.6228874498941915e-16, 3.6384366559073444e-16,
    3.65403156156137e-16, 3.669673780588701e-16, 3.685364952894914e-16, 3.7011067458828983e-16,
    3.716900855823823e-16, 3.7327490092779435e-16, 3.7486529645684887e-16, 3.7646145133120287e-16,
    3.7806354820089604e-16, 3.7967177336979443e-16, 3.8128631696783774e-16, 3.829073731305243e-16,
    3.8453514018609596e-16, 3.8616982085091493e-16, 3.878116224335587e-16, 3.894607570481926e-16,
    3.9111744183782054e-16, 3.9278189920805415e-16, 3.944543570720877e-16, 3.9613504910761354e-16,
    3.9782421502646826e-16, 3.995221008578565e-16, 4.012289592460629e-16, 4.029450497636328e-16,
    4.04670639241075e-16, 4.0640600211422504e-16, 4.0815142079049387e-16, 4.0990718603532664e-16,
    4.1167359738030257e-16, 4.134509635544236e-16, 4.1523960294026883e-16, 4.170398440568316e-16,
    4.1885202607101123e-16, 4.206764993399015e-16, 4.2251362598620494e-16, 4.243637805093078e-16,
    4.262273504347798e-16, 4.2810473700531167e-16, 4.2999635591638323e-16, 4.3190263810026294e-16,
    4.338240305622791e-16, 4.357609972736849e-16, 4.3771402012585875e-16, 4.3968359995105214e-16,
    4.4167025761542035e-16, 4.4367453519065673e-16, 4.456969972112043e-16, 4.477382320247534e-16,
    4.49798853244555e-16, 4.518795013130059e-16, 4.539808451870034e-16, 4.561035841567422e-16,
    4.582484498109567e-16, 4.604162081631153e-16, 4.626076619547846e-16, 4.648236531543207e-16,
    4.670650656712631e-16, 4.693328283093329e-16, 4.716279179838351e-16, 4.739513632325867e-16,
    4.763042480533137e-16, 4.786877161048723e-16, 4.811029753147417e-16, 4.835513029411525e-16,
    4.860340511450812e-16, 4.885526531353603e-16, 4.91108629959527e-16, 4.937035980240335e-16,
    4.963392774403987e-16, 4.990175013091822e-16, 5.017402260718089e-16, 5.045095430818727e-16,
    5.073276915733542e-16, 5.101970732341562e-16, 5.131202686306784e-16, 5.161000557743228e-16,
    5.191394311757699e-16, 5.222416338000234e-16, 5.254101724177597e-16, 5.286488569504945e-16,
    5.3196183453384e-16, 5.353536311816497e-16, 5.388292001334053e-16, 5.423939782201712e-16,
    5.46053951907478e-16, 5.498157350892814e-16, 5.536866612467876e-16, 5.576748932926576e-16,
    5.617895553555417e-16, 5.660408920082422e-16, 5.704404621291389e-16, 5.750013768919895e-16,
    5.797385945724594e-16, 5.846692893455479e-16, 5.898133176477899e-16, 5.951938149641444e-16,
    6.008379696271908e-16, 6.067780409333449e-16, 6.130527208725282e-16, 6.197089894581626e-16,
    6.268046963301284e-16, 6.344122407127506e-16, 6.426239659548055e-16, 6.515603317344994e-16,
    6.613827885097664e-16, 6.723150462505587e-16, 6.846803417564259e-16, 6.98971833638762e-16,
    7.159994934830664e-16, 7.372424301798799e-16, 7.658936370805573e-16, 8.113849337656484e-16,
];

const FI: [f64; 256] = [
    1.0, 0.9771017012676716, 0.9598790918001067, 0.9451989534422996,
    0.9320600759592305, 0.919991505039347, 0.9087264400521309, 0.8980959218983434,
    0.8879846607558334, 0.8783096558089174, 0.869008688036857, 0.8600336211963315,
    0.851346258458678, 0.8429156531122042, 0.8347162929868834, 0.8267268339462214,
    0.8189291916037024, 0.8113078743126563, 0.8038494831709643, 0.796542330422959,
    0.7893761435660246, 0.7823418326548025, 0.7754313049811872, 0.7686373157984863,
    0.7619533468367954, 0.7553735065070961, 0.7488924472191568, 0.742505296340151,
    0.7362075981268627, 0.7299952645614762, 0.7238645334686302, 0.717811932630722,
    0.7118342488782484, 0.7059285013327543, 0.7000919181365116, 0.6943219161261167,
    0.6886160830046718, 0.6829721616449949, 0.6773880362187735, 0.6718617198970821,
    0.6663913439087501, 0.6609751477766631, 0.6556114705796973, 0.6502987431108167,
    0.6450354808208223, 0.6398202774530566, 0.6346517992876236, 0.6295287799248367,
    0.6244500155470265, 0.6194143606058343, 0.6144207238889139, 0.6094680649257734,
    0.6045553906974678, 0.5996817526191253, 0.5948462437679874, 0.590047996332826,
    0.5852861792633715, 0.5805599961007909, 0.5758686829723537, 0.5712115067352532,
    0.5665877632561644, 0.5619967758145243, 0.557437893618766, 0.5529104904258323,
    0.5484139632552658, 0.5439477311900263, 0.5395112342569521, 0.5351039323804576,
    0.5307253044036621, 0.5263748471716845, 0.5220520746723218, 0.5177565172297564,
    0.513487720747327, 0.5092452459957479, 0.5050286679434681, 0.5008375751261487,
    0.4966715690524897, 0.49253026364386854, 0.48841328470545803, 0.4843202694266833,
    0.48025086590904675, 0.47620473271950586, 0.4721815384677302, 0.4681809614056936,
    0.46420268904817436, 0.46024641781284287, 0.45631185267871643, 0.4523987068618485,
    0.44850670150720306, 0.4446355653957394, 0.440785034665804, 0.43695485254798555,
    0.43314476911265226, 0.4293545410294414, 0.42558393133802197, 0.4218327092294959,
    0.4181006498378482, 0.4143875340408911, 0.41069314827018816, 0.40701728432947337,
    0.4033597392211145, 0.3997203149801972, 0.39609881851583245, 0.3924950614593156,
    0.3889088600187887, 0.3853400348400773, 0.38178841087339366, 0.3782538172456192,
    0.37473608713789114, 0.3712350576682395, 0.3677505697790326, 0.36428246812900406,
    0.36083060098964803, 0.3573948201457805, 0.3539749808000768, 0.3505709414814061,
    0.34718256395679364, 0.3438097131468507, 0.34045225704452187, 0.33711006663700605,
    0.33378301583071845, 0.3304709813791636, 0.3271738428136014, 0.3238914823763911,
    0.32062378495690536, 0.3173706380299136, 0.3141319315963372, 0.3109075581262865,
    0.30769741250429206, 0.30450139197665, 0.30131939610080305, 0.2981513266966855,
    0.2949970877999618, 0.2918565856170952, 0.2887297284821829, 0.28561642681550176,
    0.2825165930837076, 0.27943014176163794, 0.2763569892956683, 0.27329705406857707,
    0.27025025636587546, 0.26721651834356147, 0.2641957639972612, 0.2611879191327212,
    0.25819291133761924, 0.25521066995466196, 0.2522411260559422, 0.24928421241852852,
    0.24633986350126383, 0.2434080154227503, 0.2404886059405006, 0.2375815744312381,
    0.23468686187233, 0.23180441082433872, 0.22893416541468034, 0.22607607132238028,
    0.22323007576391748, 0.220396127480152, 0.21757417672433113, 0.21476417525117358,
    0.21196607630703018, 0.20917983462112508, 0.2064054063978808, 0.2036427493103349,
    0.2008918224946566, 0.19815258654577514, 0.1954250035141343, 0.19270903690358918,
    0.19000465167046499, 0.1873118142238003, 0.18463049242679927, 0.18196065559952251,
    0.17930227452284758, 0.17665532144373486, 0.17401977008183855, 0.17139559563750575,
    0.1687827748012113, 0.1661812857644819, 0.16359110823236558, 0.161012223437511,
    0.15844461415592428, 0.1558882647244792, 0.15334316106026286, 0.15080929068184568,
    0.14828664273257455, 0.14577520800599403, 0.14327497897351346, 0.1407859498144447,
    0.13830811644855073, 0.13584147657125376, 0.13338602969166916, 0.13094177717364436,
    0.12850872227999957, 0.1260868702201859, 0.12367622820159657, 0.1212768054847903,
    0.11888861344291006, 0.11651166562561087, 0.11414597782783849, 0.11179156816383809,
    0.1094484571468118, 0.1071166677746838, 0.10479622562248707, 0.10248715894193525,
    0.10018949876881002, 0.09790327903886246, 0.095628536713009, 0.09336531191269101,
    0.09111364806637376, 0.08887359206827589, 0.08664519445055807, 0.08442850957035347,
    0.0822235958132029, 0.08003051581466307, 0.07784933670209612, 0.07568013035892718,
    0.07352297371398132, 0.0713779490588904, 0.06924514439700676, 0.0671246538277885,
    0.0650165779712429, 0.06292102443775814, 0.06083810834953988, 0.05876795292093374,
    0.0567106901062029, 0.05466646132488892, 0.05263541827679219, 0.05061772386094778,
    0.04861355321586854, 0.04662309490193038, 0.044646552251294463, 0.04268414491647446,
    0.04073611065594094, 0.03880270740452615, 0.036884215688567305, 0.034980941461716125,
    0.03309321945857858, 0.0312214171919203, 0.02936593975813336, 0.027527235669603113,
    0.02570580400854891, 0.02390220330579588, 0.02211706270730885, 0.02035109623004451,
    0.018605121275724622, 0.016880083152543142, 0.01517708830793531, 0.013497450601739867,
    0.011842757857907879, 0.010214971439701459, 0.008616582769398726, 0.007050875471373222,
    0.0055224032992509916, 0.0040379725933630236, 0.0026090727461021593, 0.001260285930498598,
];

impl Generator {
    /// `Generator.standard_exponential()`: NumPy's exponential ziggurat (its
    /// default `zig` method), with the `log1p(-u)` tail NumPy uses.
    pub fn standard_exponential(&mut self) -> f64 {
        loop {
            let mut ri = self.next_u64();
            ri >>= 3;
            let idx = (ri & 0xff) as usize;
            ri >>= 8;
            let x = ri as f64 * WE[idx];
            if ri < KE[idx] {
                return x;
            }
            if idx == 0 {
                return ZIGGURAT_EXP_R - (-self.random()).ln_1p();
            } else if (FE[idx - 1] - FE[idx]) * self.random() + FE[idx] < (-x).exp() {
                return x;
            }
        }
    }

    /// `Generator.exponential(scale)`: `scale * standard_exponential()`.
    pub fn exponential(&mut self, scale: f64) -> f64 {
        scale * self.standard_exponential()
    }

    /// `n` draws of [`Generator::exponential`], as `exponential(scale, n)`.
    pub fn exponential_n(&mut self, scale: f64, n: usize) -> Vec<f64> {
        (0..n).map(|_| self.exponential(scale)).collect()
    }
}

// NumPy's exponential ziggurat tables (ke_double, we_double and fe_double
// in numpy/random/src/distributions/ziggurat_constants.h).
const KE: [u64; 256] = [
    0x001C5214272497C6, 0x0000000000000000, 0x00137D5BD79C317E, 0x00186EF58E3F3C10, 0x001A9BB7320EB0AE, 0x001BD127F719447C,
    0x001C951D0F88651A, 0x001D1BFE2D5C3972, 0x001D7E5BD56B18B2, 0x001DC934DD172C70, 0x001E0409DFAC9DC8, 0x001E337B71D47836,
    0x001E5A8B177CB7A2, 0x001E7B42096F046C, 0x001E970DAF08AE3E, 0x001EAEF5B14EF09E, 0x001EC3BD07B46556, 0x001ED5F6F08799CE,
    0x001EE614AE6E5688, 0x001EF46ECA361CD0, 0x001F014B76DDD4A4, 0x001F0CE313A796B6, 0x001F176369F1F77A, 0x001F20F20C452570,
    0x001F29AE1951A874, 0x001F31B18FB95532, 0x001F39125157C106, 0x001F3FE2EB6E694C, 0x001F463332D788FA, 0x001F4C10BF1D3A0E,
    0x001F51874C5C3322, 0x001F56A109C3ECC0, 0x001F5B66D9099996, 0x001F5FE08210D08C, 0x001F6414DD445772, 0x001F6809F6859678,
    0x001F6BC52A2B02E6, 0x001F6F4B3D32E4F4, 0x001F72A07190F13A, 0x001F75C8974D09D6, 0x001F78C71B045CC0, 0x001F7B9F12413FF4,
    0x001F7E5346079F8A, 0x001F80E63BE21138, 0x001F835A3DAD9162, 0x001F85B16056B912, 0x001F87ED89B24262, 0x001F8A10759374FA,
    0x001F8C1BBA3D39AC, 0x001F8E10CC45D04A, 0x001F8FF102013E16, 0x001F91BD968358E0, 0x001F9377AC47AFD8, 0x001F95204F8B64DA,
    0x001F96B878633892, 0x001F98410C968892, 0x001F99BAE146BA80, 0x001F9B26BC697F00, 0x001F9C85561B717A, 0x001F9DD759CFD802,
    0x001F9F1D6761A1CE, 0x001FA058140936C0, 0x001FA187EB3A3338, 0x001FA2AD6F6BC4FC, 0x001FA3C91ACE0682, 0x001FA4DB5FEE6AA2,
    0x001FA5E4AA4D097C, 0x001FA6E55EE46782, 0x001FA7DDDCA51EC4, 0x001FA8CE7CE6A874, 0x001FA9B793CE5FEE, 0x001FAA9970ADB858,
    0x001FAB745E588232, 0x001FAC48A3740584, 0x001FAD1682BF9FE8, 0x001FADDE3B5782C0, 0x001FAEA008F21D6C, 0x001FAF5C2418B07E,
    0x001FB012C25B7A12, 0x001FB0C41681DFF4, 0x001FB17050B6F1FA, 0x001FB2179EB2963A, 0x001FB2BA2BDFA84A, 0x001FB358217F4E18,
    0x001FB3F1A6C9BE0C, 0x001FB486E10CACD6, 0x001FB517F3C793FC, 0x001FB5A500C5FDAA, 0x001FB62E2837FE58, 0x001FB6B388C9010A,
    0x001FB7353FB50798, 0x001FB7B368DC7DA8, 0x001FB82E1ED6BA08, 0x001FB8A57B0347F6, 0x001FB919959A0F74, 0x001FB98A85BA7204,
    0x001FB9F861796F26, 0x001FBA633DEEE286, 0x001FBACB2F41EC16, 0x001FBB3048B49144, 0x001FBB929CAEA4E2, 0x001FBBF23CC8029E,
    0x001FBC4F39D22994, 0x001FBCA9A3E140D4, 0x001FBD018A548F9E, 0x001FBD56FBDE729C, 0x001FBDAA068BD66A, 0x001FBDFAB7CB3F40,
    0x001FBE491C7364DE, 0x001FBE9540C9695E, 0x001FBEDF3086B128, 0x001FBF26F6DE6174, 0x001FBF6C9E828AE2, 0x001FBFB031A904C4,
    0x001FBFF1BA0FFDB0, 0x001FC03141024588, 0x001FC06ECF5B54B2, 0x001FC0AA6D8B1426, 0x001FC0E42399698A, 0x001FC11BF9298A64,
    0x001FC151F57D1942, 0x001FC1861F770F4A, 0x001FC1B87D9E74B4, 0x001FC1E91620EA42, 0x001FC217EED505DE, 0x001FC2450D3C83FE,
    0x001FC27076864FC2, 0x001FC29A2F90630E, 0x001FC2C23CE98046, 0x001FC2E8A2D2C6B4, 0x001FC30D654122EC, 0x001FC33087DE9C0E,
    0x001FC3520E0B7EC6, 0x001FC371FADF66F8, 0x001FC390512A2886, 0x001FC3AD137497FA, 0x001FC3C844013348, 0x001FC3E1E4CCAB40,
    0x001FC3F9F78E4DA8, 0x001FC4107DB85060, 0x001FC4257877FD68, 0x001FC438E8B5BFC6, 0x001FC44ACF15112A, 0x001FC45B2BF447E8,
    0x001FC469FF6C4504, 0x001FC477495001B2, 0x001FC483092BFBB8, 0x001FC48D3E457FF6, 0x001FC495E799D21A, 0x001FC49D03DD30B0,
    0x001FC4A29179B432, 0x001FC4A68E8E07FC, 0x001FC4A8F8EBFB8C, 0x001FC4A9CE16EA9E, 0x001FC4A90B41FA34, 0x001FC4A6AD4E28A0,
    0x001FC4A2B0C82E74, 0x001FC49D11E62DE2, 0x001FC495CC852DF4, 0x001FC48CDC265EC0, 0x001FC4823BEC237A, 0x001FC475E696DEE6,
    0x001FC467D6817E82, 0x001FC458059DC036, 0x001FC4466D702E20, 0x001FC433070BCB98, 0x001FC41DCB0D6E0E, 0x001FC406B196BBF6,
    0x001FC3EDB248CB62, 0x001FC3D2C43E593C, 0x001FC3B5DE0591B4, 0x001FC396F599614C, 0x001FC376005A4592, 0x001FC352F3069370,
    0x001FC32DC1B22818, 0x001FC3065FBD7888, 0x001FC2DCBFCBF262, 0x001FC2B0D3B99F9E, 0x001FC2828C8FFCF0, 0x001FC251DA79F164,
    0x001FC21EACB6D39E, 0x001FC1E8F18C6756, 0x001FC1B09637BB3C, 0x001FC17586DCCD10, 0x001FC137AE74D6B6, 0x001FC0F6F6BB2414,
    0x001FC0B348184DA4, 0x001FC06C898BAFF0, 0x001FC022A092F364, 0x001FBFD5710F72B8, 0x001FBF84DD29488E, 0x001FBF30C52FC60A,
    0x001FBED907770CC6, 0x001FBE7D80327DDA, 0x001FBE1E094BA614, 0x001FBDBA7A354408, 0x001FBD52A7B9F826, 0x001FBCE663C6201A,
    0x001FBC757D2C4DE4, 0x001FBBFFBF63B7AA, 0x001FBB84F23FE6A2, 0x001FBB04D9A0D18C, 0x001FBA7F351A70AC, 0x001FB9F3BF92B618,
    0x001FB9622ED4ABFC, 0x001FB8CA33174A16, 0x001FB82B76765B54, 0x001FB7859C5B895C, 0x001FB6D840D55594, 0x001FB622F7D96942,
    0x001FB5654C6F37E0, 0x001FB49EBFBF69D2, 0x001FB3CEC803E746, 0x001FB2F4CF539C3E, 0x001FB21032442852, 0x001FB1203E5A9604,
    0x001FB0243042E1C2, 0x001FAF1B31C479A6, 0x001FAE045767E104, 0x001FACDE9DBF2D72, 0x001FABA8E640060A, 0x001FAA61F399FF28,
    0x001FA908656F66A2, 0x001FA79AB3508D3C, 0x001FA61726D1F214, 0x001FA47BD48BEA00, 0x001FA2C693C5C094, 0x001FA0F4F47DF314,
    0x001F9F04336BBE0A, 0x001F9CF12B79F9BC, 0x001F9AB84415ABC4, 0x001F98555B782FB8, 0x001F95C3ABD03F78, 0x001F92FDA9CEF1F2,
    0x001F8FFCDA9AE41C, 0x001F8CB99E7385F8, 0x001F892AEC479606, 0x001F8545F904DB8E, 0x001F80FDC336039A, 0x001F7C427839E926,
    0x001F7700A3582ACC, 0x001F71200F1A241C, 0x001F6A8234B7352A, 0x001F630000A8E266, 0x001F5A66904FE3C4, 0x001F50724ECE1172,
    0x001F44C7665C6FDA, 0x001F36E5A38A59A2, 0x001F26143450340A, 0x001F113E047B0414, 0x001EF6AEFA57CBE6, 0x001ED38CA188151E,
    0x001EA2A61E122DB0, 0x001E5961C78B267C, 0x001DDDF62BAC0BB0, 0x001CDB4DD9E4E8C0,
];

const WE: [f64; 256] = [
    9.655740063209182975e-16, 7.089014243955414331e-18, 1.163941249669122378e-17, 1.524391512353216015e-17,
    1.833284885723743916e-17, 2.108965109464486630e-17, 2.361128077843138196e-17, 2.595595772310893952e-17,
    2.816173554197752338e-17, 3.025504130321382330e-17, 3.225508254836375280e-17, 3.417632340185027033e-17,
    3.602996978734452488e-17, 3.782490776869649048e-17, 3.956832198097553231e-17, 4.126611778175946428e-17,
    4.292321808442525631e-17, 4.454377743282371417e-17, 4.613133981483185932e-17, 4.768895725264635940e-17,
    4.921928043727962847e-17, 5.072462904503147014e-17, 5.220704702792671737e-17, 5.366834661718192181e-17,
    5.511014372835094717e-17, 5.653388673239667134e-17, 5.794088004852766616e-17, 5.933230365208943081e-17,
    6.070922932847179572e-17, 6.207263431163193485e-17, 6.342341280303076511e-17, 6.476238575956142121e-17,
    6.609030925769405241e-17, 6.740788167872722244e-17, 6.871574991183812442e-17, 7.001451473403929616e-17,
    7.130473549660643409e-17, 7.258693422414648352e-17, 7.386159921381791997e-17, 7.512918820723728089e-17,
    7.639013119550825792e-17, 7.764483290797848102e-17, 7.889367502729790548e-17, 8.013701816675454434e-17,
    8.137520364041762206e-17, 8.260855505210038174e-17, 8.383737972539139383e-17, 8.506196999385323132e-17,
    8.628260436784112996e-17, 8.749954859216182511e-17, 8.871305660690252281e-17, 8.992337142215357066e-17,
    9.113072591597909173e-17, 9.233534356381788123e-17, 9.353743910649128938e-17, 9.473721916312949566e-17,
    9.593488279457997317e-17, 9.713062202221521206e-17, 9.832462230649511362e-17, 9.951706298915071878e-17,
    1.007081177024294931e-16, 1.018979547484694078e-16, 1.030867374515421954e-16, 1.042746244856188556e-16,
    1.054617701794576406e-16, 1.066483248011914702e-16, 1.078344348241948498e-16, 1.090202431758350473e-16,
    1.102058894705578110e-16, 1.113915102286197502e-16, 1.125772390816567488e-16, 1.137632069661684705e-16,
    1.149495423059009298e-16, 1.161363711840218308e-16, 1.173238175059045788e-16, 1.185120031532669434e-16,
    1.197010481303465158e-16, 1.208910707027385520e-16, 1.220821875294706151e-16, 1.232745137888415193e-16,
    1.244681632985112523e-16, 1.256632486302898513e-16, 1.268598812200397542e-16, 1.280581714730749379e-16,
    1.292582288654119552e-16, 1.304601620412028847e-16, 1.316640789066572582e-16, 1.328700867207380889e-16,
    1.340782921828999433e-16, 1.352888015181175458e-16, 1.365017205594397770e-16, 1.377171548282880964e-16,
    1.389352096127063919e-16, 1.401559900437571538e-16, 1.413796011702485188e-16, 1.426061480319665444e-16,
    1.438357357315790180e-16, 1.450684695053687684e-16, 1.463044547929475721e-16, 1.475437973060951633e-16,
    1.487866030968626066e-16, 1.500329786250736949e-16, 1.512830308253539427e-16, 1.525368671738125550e-16,
    1.537945957544996933e-16, 1.550563253257577148e-16, 1.563221653865837505e-16, 1.575922262431176140e-16,
    1.588666190753684151e-16, 1.601454560042916733e-16, 1.614288501593278662e-16, 1.627169157465130500e-16,
    1.640097681172717950e-16, 1.653075238380036909e-16, 1.666103007605742067e-16, 1.679182180938228863e-16,
    1.692313964762022267e-16, 1.705499580496629830e-16, 1.718740265349031656e-16, 1.732037273081008369e-16,
    1.745391874792533975e-16, 1.758805359722491379e-16, 1.772279036068006489e-16, 1.785814231823732619e-16,
    1.799412295642463721e-16, 1.813074597718501559e-16, 1.826802530695252266e-16, 1.840597510598587828e-16,
    1.854460977797569461e-16, 1.868394397994192684e-16, 1.882399263243892051e-16, 1.896477093008616722e-16,
    1.910629435244376536e-16, 1.924857867525243818e-16, 1.939163998205899420e-16, 1.953549467624909132e-16,
    1.968015949351037382e-16, 1.982565151475019047e-16, 1.997198817949342081e-16, 2.011918729978734671e-16,
    2.026726707464198289e-16, 2.041624610503588774e-16, 2.056614340951917875e-16, 2.071697844044737034e-16,
    2.086877110088159721e-16, 2.102154176219292789e-16, 2.117531128241075913e-16, 2.133010102535779087e-16,
    2.148593288061663316e-16, 2.164282928437604723e-16, 2.180081324120784027e-16, 2.195990834682870728e-16,
    2.212013881190495942e-16, 2.228152948696180545e-16, 2.244410588846308588e-16, 2.260789422613173739e-16,
    2.277292143158621037e-16, 2.293921518837311354e-16, 2.310680396348213318e-16, 2.327571704043534613e-16,
    2.344598455404957859e-16, 2.361763752697773994e-16, 2.379070790814276700e-16, 2.396522861318623520e-16,
    2.414123356706293277e-16, 2.431875774892255956e-16, 2.449783723943070217e-16, 2.467850927069288738e-16,
    2.486081227895851719e-16, 2.504478596029557040e-16, 2.523047132944217013e-16, 2.541791078205812227e-16,
    2.560714816061770759e-16, 2.579822882420530896e-16, 2.599119972249746917e-16, 2.618610947423924219e-16,
    2.638300845054942823e-16, 2.658194886341845120e-16, 2.678298485979525166e-16, 2.698617262169488933e-16,
    2.719157047279818500e-16, 2.739923899205814823e-16, 2.760924113487617126e-16, 2.782164236246436081e-16,
    2.803651078006983464e-16, 2.825391728480253184e-16, 2.847393572388174091e-16, 2.869664306419817679e-16,
    2.892211957417995598e-16, 2.915044901905293183e-16, 2.938171887070028633e-16, 2.961602053345465687e-16,
    2.985344958730045276e-16, 3.009410605012618141e-16, 3.033809466085003416e-16, 3.058552518544860874e-16,
    3.083651274815310004e-16, 3.109117819034266344e-16, 3.134964845996663118e-16, 3.161205703467105734e-16,
    3.187854438219713117e-16, 3.214925846206797361e-16, 3.242435527309451638e-16, 3.270399945182240440e-16,
    3.298836492772283149e-16, 3.327763564171671408e-16, 3.357200633553244075e-16, 3.387168342045505162e-16,
    3.417688593525636996e-16, 3.448784660453423890e-16, 3.480481301037442286e-16, 3.512804889222979418e-16,
    3.545783559224791863e-16, 3.579447366604276541e-16, 3.613828468219060593e-16, 3.648961323764542545e-16,
    3.684882922095621322e-16, 3.721633036080207290e-16, 3.759254510416256532e-16, 3.797793587668874387e-16,
    3.837300278789213687e-16, 3.877828785607895292e-16, 3.919437984311428867e-16, 3.962191980786774996e-16,
    4.006160751056541688e-16, 4.051420882956573177e-16, 4.098056438903062509e-16, 4.146159964290904582e-16,
    4.195833672073398926e-16, 4.247190841824385048e-16, 4.300357481667470702e-16, 4.355474314693952008e-16,
    4.412699169036069903e-16, 4.472209874259932285e-16, 4.534207798565834480e-16, 4.598922204905932469e-16,
    4.666615664711475780e-16, 4.737590853262492027e-16, 4.812199172829237933e-16, 4.890851827392209900e-16,
    4.974034236191939753e-16, 5.062325072144159699e-16, 5.156421828878082953e-16, 5.257175802022274839e-16,
    5.365640977112021618e-16, 5.483144034258703912e-16, 5.611387454675159622e-16, 5.752606481503331688e-16,
    5.909817641652102998e-16, 6.087231416180907671e-16, 6.290979034877557049e-16, 6.530492053564040799e-16,
    6.821393079028928626e-16, 7.192444966089361564e-16, 7.706095350032096755e-16, 8.545517038584027421e-16,
];

const FE: [f64; 256] = [
    1.000000000000000000e+00, 9.381436808621747003e-01, 9.004699299257464817e-01, 8.717043323812035949e-01,
    8.477855006239896074e-01, 8.269932966430503241e-01, 8.084216515230083777e-01, 7.915276369724956185e-01,
    7.759568520401155522e-01, 7.614633888498962833e-01, 7.478686219851951034e-01, 7.350380924314234843e-01,
    7.228676595935720206e-01, 7.112747608050760117e-01, 7.001926550827881623e-01, 6.895664961170779872e-01,
    6.793505722647653622e-01, 6.695063167319247333e-01, 6.600008410789997004e-01, 6.508058334145710999e-01,
    6.418967164272660897e-01, 6.332519942143660652e-01, 6.248527387036659775e-01, 6.166821809152076561e-01,
    6.087253820796220127e-01, 6.009689663652322267e-01, 5.934009016917334289e-01, 5.860103184772680329e-01,
    5.787873586028450257e-01, 5.717230486648258170e-01, 5.648091929124001709e-01, 5.580382822625874484e-01,
    5.514034165406412891e-01, 5.448982376724396115e-01, 5.385168720028619127e-01, 5.322538802630433219e-01,
    5.261042139836197284e-01, 5.200631773682335979e-01, 5.141263938147485613e-01, 5.082897764106428795e-01,
    5.025495018413477233e-01, 4.969019872415495476e-01, 4.913438695940325340e-01, 4.858719873418849144e-01,
    4.804833639304542103e-01, 4.751751930373773747e-01, 4.699448252839599771e-01, 4.647897562504261781e-01,
    4.597076156421376902e-01, 4.546961574746155033e-01, 4.497532511627549967e-01, 4.448768734145485126e-01,
    4.400651008423538957e-01, 4.353161032156365740e-01, 4.306281372884588343e-01, 4.259995411430343437e-01,
    4.214287289976165751e-01, 4.169141864330028757e-01, 4.124544659971611793e-01, 4.080481831520323954e-01,
    4.036940125305302773e-01, 3.993906844752310725e-01, 3.951369818332901573e-01, 3.909317369847971069e-01,
    3.867738290841376547e-01, 3.826621814960098344e-01, 3.785957594095807899e-01, 3.745735676159021588e-01,
    3.705946484351460013e-01, 3.666580797815141568e-01, 3.627629733548177748e-01, 3.589084729487497794e-01,
    3.550937528667874599e-01, 3.513180164374833381e-01, 3.475804946216369817e-01, 3.438804447045024082e-01,
    3.402171490667800224e-01, 3.365899140286776059e-01, 3.329980687618089852e-01, 3.294409642641363267e-01,
    3.259179723935561879e-01, 3.224284849560891675e-01, 3.189719128449572394e-01, 3.155476852271289490e-01,
    3.121552487741795501e-01, 3.087940669345601852e-01, 3.054636192445902565e-01, 3.021634006756935276e-01,
    2.988929210155817917e-01, 2.956517042812611962e-01, 2.924392881618925744e-01, 2.892552234896777485e-01,
    2.860990737370768255e-01, 2.829704145387807457e-01, 2.798688332369729248e-01, 2.767939284485173568e-01,
    2.737453096528029706e-01, 2.707225967990600224e-01, 2.677254199320447947e-01, 2.647534188350622042e-01,
    2.618062426893629779e-01, 2.588835497490162285e-01, 2.559850070304153791e-01, 2.531102900156294577e-01,
    2.502590823688622956e-01, 2.474310756653276266e-01, 2.446259691318921070e-01, 2.418434693988772144e-01,
    2.390832902624491774e-01, 2.363451524570596429e-01, 2.336287834374333461e-01, 2.309339171696274118e-01,
    2.282602939307167011e-01, 2.256076601166840667e-01, 2.229757680581201940e-01, 2.203643758433594946e-01,
    2.177732471487005272e-01, 2.152021510753786837e-01, 2.126508619929782795e-01, 2.101191593889882581e-01,
    2.076068277242220372e-01, 2.051136562938377095e-01, 2.026394390937090173e-01, 2.001839746919112650e-01,
    1.977470661050988732e-01, 1.953285206795632167e-01, 1.929281499767713515e-01, 1.905457696631953912e-01,
    1.881811994042543179e-01, 1.858342627621971110e-01, 1.835047870977674633e-01, 1.811926034754962889e-01,
    1.788975465724783054e-01, 1.766194545904948843e-01, 1.743581691713534942e-01, 1.721135353153200598e-01,
    1.698854013025276610e-01, 1.676736186172501919e-01, 1.654780418749360049e-01, 1.632985287519018169e-01,
    1.611349399175920349e-01, 1.589871389693142123e-01, 1.568549923693652315e-01, 1.547383693844680830e-01,
    1.526371420274428570e-01, 1.505511850010398944e-01, 1.484803756438667910e-01, 1.464245938783449441e-01,
    1.443837221606347754e-01, 1.423576454324722018e-01, 1.403462510748624548e-01, 1.383494288635802039e-01,
    1.363670709264288572e-01, 1.343990717022136294e-01, 1.324453279013875218e-01, 1.305057384683307731e-01,
    1.285802045452281717e-01, 1.266686294375106714e-01, 1.247709185808309612e-01, 1.228869795095451356e-01,
    1.210167218266748335e-01, 1.191600571753276827e-01, 1.173168992115555670e-01, 1.154871635786335338e-01,
    1.136707678827443141e-01, 1.118676316700562973e-01, 1.100776764051853845e-01, 1.083008254510337970e-01,
    1.065370040500016602e-01, 1.047861393065701724e-01, 1.030481601712577161e-01, 1.013229974259536315e-01,
    9.961058367063713170e-02, 9.791085331149219917e-02, 9.622374255043279756e-02, 9.454918937605585882e-02,
    9.288713355604354127e-02, 9.123751663104015530e-02, 8.960028191003285847e-02, 8.797537446727021759e-02,
    8.636274114075691288e-02, 8.476233053236811865e-02, 8.317409300963238272e-02, 8.159798070923741931e-02,
    8.003394754231990538e-02, 7.848194920160642130e-02, 7.694194317048050347e-02, 7.541388873405840965e-02,
    7.389774699236474620e-02, 7.239348087570873780e-02, 7.090105516237182881e-02, 6.942043649872875477e-02,
    6.795159342193660135e-02, 6.649449638533977414e-02, 6.504911778675374900e-02, 6.361543199980733421e-02,
    6.219341540854099459e-02, 6.078304644547963265e-02, 5.938430563342026597e-02, 5.799717563120065922e-02,
    5.662164128374287675e-02, 5.525768967669703741e-02, 5.390531019604608703e-02, 5.256449459307169225e-02,
    5.123523705512628146e-02, 4.991753428270637172e-02, 4.861138557337949667e-02, 4.731679291318154762e-02,
    4.603376107617516977e-02, 4.476229773294328196e-02, 4.350241356888818328e-02, 4.225412241331623353e-02,
    4.101744138041481941e-02, 3.979239102337412542e-02, 3.857899550307485742e-02, 3.737728277295936097e-02,
    3.618728478193142251e-02, 3.500903769739741045e-02, 3.384258215087432992e-02, 3.268796350895953468e-02,
    3.154523217289360859e-02, 3.041444391046660423e-02, 2.929566022463739317e-02, 2.818894876397863569e-02,
    2.709438378095579969e-02, 2.601204664513421735e-02, 2.494202641973178314e-02, 2.388442051155817078e-02,
    2.283933540638524023e-02, 2.180688750428358066e-02, 2.078720407257811723e-02, 1.978042433800974303e-02,
    1.878670074469603046e-02, 1.780620041091136169e-02, 1.683910682603994777e-02, 1.588562183997316302e-02,
    1.494596801169114850e-02, 1.402039140318193759e-02, 1.310916493125499106e-02, 1.221259242625538123e-02,
    1.133101359783459695e-02, 1.046481018102997894e-02, 9.614413642502209895e-03, 8.780314985808975251e-03,
    7.963077438017040002e-03, 7.163353183634983863e-03, 6.381905937319179087e-03, 5.619642207205483020e-03,
    4.877655983542392333e-03, 4.157295120833795314e-03, 3.460264777836904049e-03, 2.788798793574076128e-03,
    2.145967743718906265e-03, 1.536299780301572356e-03, 9.672692823271745359e-04, 4.541343538414967652e-04,
];


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn uniform_is_numpys() {
        // np.random.default_rng(5).uniform(-0.3, 0.3, 3)
        let mut g = Generator::new(5);
        assert_eq!(g.uniform_n(-0.3, 0.3, 3), vec![0.1830017542472281, 0.18476447384189626, 0.009195336625285211]);
    }

    #[test]
    fn normal_is_numpys() {
        // np.random.default_rng(6).normal(0, 2.5, 3)
        let mut g = Generator::new(6);
        assert_eq!(g.normal_n(0.0, 2.5, 3), vec![2.6327893862168956, 4.441228259542482, -6.383229596142534]);
        // np.random.default_rng(0).standard_normal(304)[-1]: the first draw
        // from the tail beyond the base layer.
        let mut g = Generator::new(0);
        let z: Vec<f64> = (0..304).map(|_| g.standard_normal()).collect();
        assert_eq!(z[303], -3.772275156122734);
        assert!(z[..303].iter().all(|v| v.abs() < 3.66));
    }

    #[test]
    fn exponential_is_numpys() {
        // np.random.default_rng(9).exponential(0.45, 3)
        let mut g = Generator::new(9);
        assert_eq!(g.exponential_n(0.45, 3), vec![1.4800084077300688, 0.22211810197420054, 0.42343184590712624]);
        // np.random.default_rng(0).standard_exponential(296)[-1]: the first
        // draw from the tail beyond the base layer.
        let mut g = Generator::new(0);
        let x: Vec<f64> = (0..296).map(|_| g.standard_exponential()).collect();
        assert_eq!(x[295], 8.128754306660403);
        assert!(x[..295].iter().all(|v| *v < 7.7));
    }

    #[test]
    fn integers_are_numpys() {
        // np.random.default_rng(7).integers(-3, 9, 5), then .integers(0, 2**40)
        let mut g = Generator::new(7);
        let a: Vec<i64> = (0..5).map(|_| g.integers(-3, 9)).collect();
        assert_eq!(a, vec![8, 4, 5, 7, 3]);
        assert_eq!(g.integers(0, 1 << 40), 247617924053);
    }
}