mod-rand 0.9.5

Tiered random number generation for Rust. Fast PRNG, process-unique seeds, and OS-backed cryptographic random in one zero-dependency library. Pick the tier appropriate to your threat model.
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
//! # Tier 1 — Fast deterministic PRNG
//!
//! Pseudorandom number generator suitable for simulation, test
//! fixtures, and non-security shuffling. Seedable so runs are
//! reproducible. **Not cryptographically secure** — outputs are
//! predictable given the seed.
//!
//! Algorithm: xoshiro256\*\* by Blackman & Vigna. State expansion from
//! a single `u64` is performed with splitmix64. See
//! <https://prng.di.unimi.it/xoshiro256starstar.c> for the canonical
//! reference.
//!
//! Performance: ~1 ns per `u64` on x86_64 (single rotation, multiply,
//! and a handful of xors per output).
//!
//! Available in `no_std`.
//!
//! ## Bounded ranges
//!
//! The `gen_range_*` family of methods produces uniformly-distributed
//! values within a caller-specified `Range` or `RangeInclusive`:
//!
//! ```
//! use mod_rand::tier1::Xoshiro256;
//!
//! let mut rng = Xoshiro256::seed_from_u64(42);
//!
//! // Half-open: [0, 100) — never returns 100.
//! let pct: u32 = rng.gen_range_u32(0..100);
//! assert!(pct < 100);
//!
//! // Inclusive: [1, 6] — die roll, can return 1, 2, 3, 4, 5, or 6.
//! let die: u32 = rng.gen_range_inclusive_u32(1..=6);
//! assert!((1..=6).contains(&die));
//! ```
//!
//! All bounded methods use Lemire's "Nearly Divisionless" rejection
//! sampling, so the output is genuinely uniform — no modulo bias.

use core::ops::{Range, RangeInclusive};

/// Golden-ratio increment used by splitmix64.
const SPLITMIX_GAMMA: u64 = 0x9E37_79B9_7F4A_7C15;

/// Polynomial-jump constants for `jump()` — equivalent to 2^128 calls
/// to `next_u64()`. Sourced verbatim from the canonical reference.
const JUMP: [u64; 4] = [
    0x180E_C6D3_3CFD_0ABA,
    0xD5A6_1266_F0C9_392C,
    0xA958_2618_E03F_C9AA,
    0x39AB_DC45_29B1_661C,
];

/// Polynomial-jump constants for `long_jump()` — equivalent to 2^192
/// calls to `next_u64()`. Sourced verbatim from the canonical reference.
const LONG_JUMP: [u64; 4] = [
    0x76E1_5D3E_FEFD_CBBF,
    0xC500_4E44_1C52_2FB3,
    0x7771_0069_854E_E241,
    0x3910_9BB0_2ACB_E635,
];

/// xoshiro256\*\* — fast, statistically sound, deterministic PRNG.
///
/// 256 bits of state. Period of `2^256 - 1`. Passes the BigCrush test
/// battery. Seedable from a single `u64` via splitmix64 expansion.
///
/// # Example
///
/// ```
/// use mod_rand::tier1::Xoshiro256;
///
/// let mut rng = Xoshiro256::seed_from_u64(42);
/// let n: u64 = rng.next_u64();
/// # let _ = n;
/// ```
///
/// # Reproducibility
///
/// The output stream is fully determined by the seed. Two generators
/// constructed with the same seed produce identical streams forever:
///
/// ```
/// use mod_rand::tier1::Xoshiro256;
///
/// let mut a = Xoshiro256::seed_from_u64(2026);
/// let mut b = Xoshiro256::seed_from_u64(2026);
/// for _ in 0..1024 {
///     assert_eq!(a.next_u64(), b.next_u64());
/// }
/// ```
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Xoshiro256 {
    state: [u64; 4],
}

impl Xoshiro256 {
    /// Construct a generator from a single 64-bit seed.
    ///
    /// The seed is expanded to 256 bits of internal state by running
    /// four rounds of splitmix64 — the seeding strategy recommended by
    /// the xoshiro authors. Any seed value, including zero, is
    /// accepted; splitmix64 cannot produce the all-zero state from a
    /// nonzero counter, and the counter starts at `seed + GAMMA`.
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let mut rng = Xoshiro256::seed_from_u64(0);
    /// let _ = rng.next_u64();
    /// ```
    #[inline]
    pub fn seed_from_u64(seed: u64) -> Self {
        let mut x = seed;
        Self {
            state: [
                splitmix64(&mut x),
                splitmix64(&mut x),
                splitmix64(&mut x),
                splitmix64(&mut x),
            ],
        }
    }

    /// Construct a generator directly from a 256-bit state.
    ///
    /// The state MUST NOT be all zero — that is the single fixed point
    /// of the xoshiro256\*\* transition. This constructor returns
    /// `None` in that case.
    ///
    /// Prefer [`seed_from_u64`](Self::seed_from_u64) for most uses;
    /// this exists for cases where you have a pre-derived 256-bit seed
    /// (e.g., from a key-derivation function).
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let rng = Xoshiro256::from_state([1, 2, 3, 4]).unwrap();
    /// # let _ = rng;
    /// assert!(Xoshiro256::from_state([0; 4]).is_none());
    /// ```
    #[inline]
    pub fn from_state(state: [u64; 4]) -> Option<Self> {
        if state == [0; 4] {
            None
        } else {
            Some(Self { state })
        }
    }

    /// Return the current internal state.
    ///
    /// Useful for checkpointing a stream so it can be resumed later
    /// via [`from_state`](Self::from_state).
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let mut rng = Xoshiro256::seed_from_u64(7);
    /// let _ = rng.next_u64();
    /// let snapshot = rng.state();
    /// let resumed = Xoshiro256::from_state(snapshot).unwrap();
    /// assert_eq!(rng, resumed);
    /// ```
    #[inline]
    pub fn state(&self) -> [u64; 4] {
        self.state
    }

    /// Produce the next 64-bit output.
    ///
    /// One call performs a single rotation, two multiplies, a shift,
    /// six xors, and one further rotation — about a nanosecond on
    /// modern x86_64.
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let mut rng = Xoshiro256::seed_from_u64(1);
    /// let n = rng.next_u64();
    /// # let _ = n;
    /// ```
    #[inline]
    pub fn next_u64(&mut self) -> u64 {
        // result = rotl(s[1] * 5, 7) * 9
        let result = self.state[1].wrapping_mul(5).rotate_left(7).wrapping_mul(9);

        let t = self.state[1] << 17;

        self.state[2] ^= self.state[0];
        self.state[3] ^= self.state[1];
        self.state[1] ^= self.state[2];
        self.state[0] ^= self.state[3];
        self.state[2] ^= t;
        self.state[3] = self.state[3].rotate_left(45);

        result
    }

    /// Produce the next 32-bit output by discarding the low half of a
    /// 64-bit draw.
    ///
    /// The high bits of xoshiro256\*\* are slightly stronger than the
    /// low bits — taking the high 32 is the recommended way to obtain
    /// a 32-bit value.
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let mut rng = Xoshiro256::seed_from_u64(1);
    /// let n: u32 = rng.next_u32();
    /// # let _ = n;
    /// ```
    #[inline]
    pub fn next_u32(&mut self) -> u32 {
        (self.next_u64() >> 32) as u32
    }

    /// Produce a value uniformly in `[0.0, 1.0)`.
    ///
    /// Uses the upper 53 bits of a `u64` output — the standard way to
    /// obtain a uniform double-precision draw without modulo bias.
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let mut rng = Xoshiro256::seed_from_u64(1);
    /// let f = rng.next_f64();
    /// assert!((0.0..1.0).contains(&f));
    /// ```
    #[inline]
    pub fn next_f64(&mut self) -> f64 {
        // 53 bits of mantissa precision. 1.0 / 2^53.
        (self.next_u64() >> 11) as f64 * (1.0 / ((1u64 << 53) as f64))
    }

    /// Fill the entire buffer with PRNG output.
    ///
    /// Equivalent to repeatedly calling [`next_u64`](Self::next_u64)
    /// and writing the little-endian bytes, but avoids the overhead of
    /// per-byte calls. The final partial chunk (1..7 bytes) is filled
    /// from a single fresh `u64`.
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let mut rng = Xoshiro256::seed_from_u64(1);
    /// let mut buf = [0u8; 32];
    /// rng.fill_bytes(&mut buf);
    /// ```
    pub fn fill_bytes(&mut self, buf: &mut [u8]) {
        let mut chunks = buf.chunks_exact_mut(8);
        for chunk in &mut chunks {
            let bytes = self.next_u64().to_le_bytes();
            chunk.copy_from_slice(&bytes);
        }
        let tail = chunks.into_remainder();
        if !tail.is_empty() {
            let bytes = self.next_u64().to_le_bytes();
            tail.copy_from_slice(&bytes[..tail.len()]);
        }
    }

    // ------------------------------------------------------------
    // Bounded-range API
    //
    // Every bounded method below is a thin wrapper around the
    // private `bounded_u64` helper, which implements Lemire's
    // "Nearly Divisionless" rejection sampling. Signed-integer
    // methods compute the span in u128 to avoid overflow at extreme
    // endpoints; the full-width inclusive range is special-cased.
    // ------------------------------------------------------------

    /// Generate a uniformly-distributed `u64` in the half-open range
    /// `[range.start, range.end)`.
    ///
    /// Uses Lemire's "Nearly Divisionless" rejection sampling so the
    /// output is genuinely uniform — there is no modulo bias.
    ///
    /// # Panics
    ///
    /// Panics if the range is empty (`range.start >= range.end`).
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let mut rng = Xoshiro256::seed_from_u64(1);
    /// let n = rng.gen_range_u64(10..20);
    /// assert!((10..20).contains(&n));
    /// ```
    #[inline]
    pub fn gen_range_u64(&mut self, range: Range<u64>) -> u64 {
        let Range { start, end } = range;
        assert!(start < end, "gen_range_u64: empty range {start}..{end}");
        let span = end - start;
        start + self.bounded_u64(span)
    }

    /// Generate a uniformly-distributed `u64` in the closed range
    /// `[range.start(), range.end()]`.
    ///
    /// # Panics
    ///
    /// Panics if the range is empty (`*range.start() > *range.end()`).
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let mut rng = Xoshiro256::seed_from_u64(1);
    /// let die = rng.gen_range_inclusive_u64(1..=6);
    /// assert!((1..=6).contains(&die));
    /// ```
    ///
    /// The full-width inclusive range `0..=u64::MAX` is supported and
    /// is equivalent to a single `next_u64()` draw.
    #[inline]
    pub fn gen_range_inclusive_u64(&mut self, range: RangeInclusive<u64>) -> u64 {
        let (start, end) = range.into_inner();
        assert!(
            start <= end,
            "gen_range_inclusive_u64: empty range {start}..={end}"
        );
        if start == 0 && end == u64::MAX {
            // span = 2^64, which doesn't fit in u64. Use a raw draw.
            return self.next_u64();
        }
        let span = end - start + 1;
        start + self.bounded_u64(span)
    }

    /// Generate a uniformly-distributed `u32` in the half-open range
    /// `[range.start, range.end)`.
    ///
    /// # Panics
    ///
    /// Panics if the range is empty.
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let mut rng = Xoshiro256::seed_from_u64(1);
    /// let pct = rng.gen_range_u32(0..100);
    /// assert!(pct < 100);
    /// ```
    #[inline]
    pub fn gen_range_u32(&mut self, range: Range<u32>) -> u32 {
        let Range { start, end } = range;
        assert!(start < end, "gen_range_u32: empty range {start}..{end}");
        let span = (end - start) as u64;
        (start as u64 + self.bounded_u64(span)) as u32
    }

    /// Generate a uniformly-distributed `u32` in the closed range
    /// `[range.start(), range.end()]`.
    ///
    /// The full-width inclusive range `0..=u32::MAX` is supported.
    ///
    /// # Panics
    ///
    /// Panics if the range is empty.
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let mut rng = Xoshiro256::seed_from_u64(1);
    /// let n = rng.gen_range_inclusive_u32(1..=100);
    /// assert!((1..=100).contains(&n));
    /// ```
    #[inline]
    pub fn gen_range_inclusive_u32(&mut self, range: RangeInclusive<u32>) -> u32 {
        let (start, end) = range.into_inner();
        assert!(
            start <= end,
            "gen_range_inclusive_u32: empty range {start}..={end}"
        );
        // span = end - start + 1 fits in u64 (max value is 2^32).
        let span = (end as u64) - (start as u64) + 1;
        (start as u64 + self.bounded_u64(span)) as u32
    }

    /// Generate a uniformly-distributed `i64` in the half-open range
    /// `[range.start, range.end)`.
    ///
    /// Negative bounds and mixed-sign ranges are supported.
    ///
    /// # Panics
    ///
    /// Panics if the range is empty.
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let mut rng = Xoshiro256::seed_from_u64(1);
    /// let n = rng.gen_range_i64(-50..50);
    /// assert!((-50..50).contains(&n));
    /// ```
    #[inline]
    pub fn gen_range_i64(&mut self, range: Range<i64>) -> i64 {
        let Range { start, end } = range;
        assert!(start < end, "gen_range_i64: empty range {start}..{end}");
        // span as u64 — works because end - start (in i128) is positive
        // and fits in u64 for any valid i64 half-open range. Maximum
        // possible span is i64::MAX - i64::MIN = 2^64 - 1.
        let span = (end as i128 - start as i128) as u64;
        let offset = self.bounded_u64(span);
        ((start as i128) + (offset as i128)) as i64
    }

    /// Generate a uniformly-distributed `i64` in the closed range
    /// `[range.start(), range.end()]`.
    ///
    /// The full-width inclusive range `i64::MIN..=i64::MAX` is
    /// supported and is equivalent to reinterpreting a raw
    /// `next_u64()` draw as `i64`.
    ///
    /// # Panics
    ///
    /// Panics if the range is empty.
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let mut rng = Xoshiro256::seed_from_u64(1);
    /// let n = rng.gen_range_inclusive_i64(-1..=1);
    /// assert!((-1..=1).contains(&n));
    /// ```
    #[inline]
    pub fn gen_range_inclusive_i64(&mut self, range: RangeInclusive<i64>) -> i64 {
        let (start, end) = range.into_inner();
        assert!(
            start <= end,
            "gen_range_inclusive_i64: empty range {start}..={end}"
        );
        if start == i64::MIN && end == i64::MAX {
            // span = 2^64. Reinterpret a raw draw.
            return self.next_u64() as i64;
        }
        // span = (end - start + 1) computed in i128 to avoid overflow.
        let span = ((end as i128) - (start as i128) + 1) as u64;
        let offset = self.bounded_u64(span);
        ((start as i128) + (offset as i128)) as i64
    }

    /// Generate a uniformly-distributed `i32` in the half-open range
    /// `[range.start, range.end)`.
    ///
    /// # Panics
    ///
    /// Panics if the range is empty.
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let mut rng = Xoshiro256::seed_from_u64(1);
    /// let n = rng.gen_range_i32(-10..10);
    /// assert!((-10..10).contains(&n));
    /// ```
    #[inline]
    pub fn gen_range_i32(&mut self, range: Range<i32>) -> i32 {
        let Range { start, end } = range;
        assert!(start < end, "gen_range_i32: empty range {start}..{end}");
        // span fits in u64 (max 2^32 - 1).
        let span = (end as i64 - start as i64) as u64;
        let offset = self.bounded_u64(span);
        ((start as i64) + (offset as i64)) as i32
    }

    /// Generate a uniformly-distributed `i32` in the closed range
    /// `[range.start(), range.end()]`.
    ///
    /// The full-width inclusive range `i32::MIN..=i32::MAX` is
    /// supported.
    ///
    /// # Panics
    ///
    /// Panics if the range is empty.
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let mut rng = Xoshiro256::seed_from_u64(1);
    /// let n = rng.gen_range_inclusive_i32(-100..=100);
    /// assert!((-100..=100).contains(&n));
    /// ```
    #[inline]
    pub fn gen_range_inclusive_i32(&mut self, range: RangeInclusive<i32>) -> i32 {
        let (start, end) = range.into_inner();
        assert!(
            start <= end,
            "gen_range_inclusive_i32: empty range {start}..={end}"
        );
        // span = (end - start + 1) fits in u64 (max 2^32).
        let span = ((end as i64) - (start as i64) + 1) as u64;
        let offset = self.bounded_u64(span);
        ((start as i64) + (offset as i64)) as i32
    }

    /// Generate a uniformly-distributed `f64` in the half-open range
    /// `[range.start, range.end)`.
    ///
    /// The implementation draws a uniform value in `[0.0, 1.0)` via
    /// [`next_f64`](Self::next_f64) and scales it linearly into the
    /// requested range. The returned value is guaranteed finite.
    ///
    /// # Panics
    ///
    /// Panics if either bound is non-finite (NaN or infinity), the
    /// range is empty (`start >= end`), or the span `end - start`
    /// overflows to infinity (e.g., `-f64::MAX..f64::MAX`).
    ///
    /// There is no `gen_range_inclusive_f64`: the probability of
    /// producing either endpoint exactly is zero for any reasonable
    /// range, so the half-open and inclusive versions are operationally
    /// identical.
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let mut rng = Xoshiro256::seed_from_u64(1);
    /// let x = rng.gen_range_f64(-1.0..1.0);
    /// assert!((-1.0..1.0).contains(&x));
    /// ```
    #[inline]
    pub fn gen_range_f64(&mut self, range: Range<f64>) -> f64 {
        let Range { start, end } = range;
        assert!(
            start.is_finite() && end.is_finite(),
            "gen_range_f64: non-finite bounds {start}..{end}"
        );
        assert!(start < end, "gen_range_f64: empty range {start}..{end}");
        let span = end - start;
        assert!(
            span.is_finite(),
            "gen_range_f64: span {start}..{end} overflows to infinity; \
             split the range or use a smaller interval"
        );
        start + self.next_f64() * span
    }

    /// Produce a uniformly-distributed `u64` in `[0, n)`.
    ///
    /// Internal helper for the bounded-range API. Implements Daniel
    /// Lemire's "Nearly Divisionless" rejection sampling (J. ACM 2019),
    /// which is unbiased — every value in `[0, n)` is equally likely.
    ///
    /// The expected number of `next_u64()` calls is approximately
    /// `1 + n / 2^64`, which is effectively 1 for any range smaller
    /// than half the `u64` space.
    ///
    /// `n` MUST be greater than zero. Public methods enforce this
    /// before calling.
    #[inline]
    fn bounded_u64(&mut self, n: u64) -> u64 {
        debug_assert!(n != 0, "bounded_u64 requires n > 0");
        // Single fast path covers ~99.99% of calls: one draw, one
        // multiply, no division.
        let mut x = self.next_u64();
        let mut m: u128 = (x as u128).wrapping_mul(n as u128);
        let mut l: u64 = m as u64;
        if l < n {
            // Rejection threshold: (-n) mod n in u64 arithmetic.
            let t: u64 = n.wrapping_neg() % n;
            while l < t {
                x = self.next_u64();
                m = (x as u128).wrapping_mul(n as u128);
                l = m as u64;
            }
        }
        (m >> 64) as u64
    }

    /// Advance the state by 2^128 calls to [`next_u64`](Self::next_u64).
    ///
    /// Use this to obtain non-overlapping parallel streams: clone the
    /// generator, call `jump()` on the clone, and the clone now
    /// produces a stream that will not collide with the original for
    /// 2^128 outputs.
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let mut a = Xoshiro256::seed_from_u64(1);
    /// let mut b = a.clone();
    /// b.jump();
    /// assert_ne!(a.next_u64(), b.next_u64());
    /// ```
    pub fn jump(&mut self) {
        self.apply_jump(&JUMP);
    }

    /// Advance the state by 2^192 calls to [`next_u64`](Self::next_u64).
    ///
    /// Use this to partition the period into 2^64 non-overlapping
    /// streams, each itself capable of 2^128 further [`jump`](Self::jump)
    /// substreams.
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let mut a = Xoshiro256::seed_from_u64(1);
    /// let mut b = a.clone();
    /// b.long_jump();
    /// assert_ne!(a.next_u64(), b.next_u64());
    /// ```
    pub fn long_jump(&mut self) {
        self.apply_jump(&LONG_JUMP);
    }

    fn apply_jump(&mut self, constants: &[u64; 4]) {
        let mut s0 = 0u64;
        let mut s1 = 0u64;
        let mut s2 = 0u64;
        let mut s3 = 0u64;

        for &word in constants {
            for b in 0..64 {
                if (word & (1u64 << b)) != 0 {
                    s0 ^= self.state[0];
                    s1 ^= self.state[1];
                    s2 ^= self.state[2];
                    s3 ^= self.state[3];
                }
                let _ = self.next_u64();
            }
        }

        self.state[0] = s0;
        self.state[1] = s1;
        self.state[2] = s2;
        self.state[3] = s3;
    }
}

/// splitmix64 — fast, full-period mixing of a 64-bit counter into a
/// 64-bit output. Used here purely to expand a single-`u64` seed into
/// the four-`u64` state required by xoshiro256\*\*.
#[inline]
fn splitmix64(x: &mut u64) -> u64 {
    *x = x.wrapping_add(SPLITMIX_GAMMA);
    let mut z = *x;
    z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
    z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
    z ^ (z >> 31)
}

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

    #[test]
    fn seed_produces_a_value() {
        let mut rng = Xoshiro256::seed_from_u64(1);
        let _ = rng.next_u64();
    }

    #[test]
    fn different_seeds_produce_different_streams() {
        let mut a = Xoshiro256::seed_from_u64(1);
        let mut b = Xoshiro256::seed_from_u64(2);
        assert_ne!(a.next_u64(), b.next_u64());
    }

    #[test]
    fn same_seed_produces_same_stream() {
        let mut a = Xoshiro256::seed_from_u64(42);
        let mut b = Xoshiro256::seed_from_u64(42);
        for _ in 0..256 {
            assert_eq!(a.next_u64(), b.next_u64());
        }
    }

    #[test]
    fn zero_seed_does_not_yield_zero_state() {
        // splitmix64 of seed=0 starts at counter = GAMMA, so the state
        // is non-zero. The first output must therefore also be nonzero
        // for any reasonable initial state.
        let mut rng = Xoshiro256::seed_from_u64(0);
        assert_ne!(rng.state(), [0; 4]);
        assert_ne!(rng.next_u64(), 0);
    }

    #[test]
    fn from_state_rejects_all_zero() {
        assert!(Xoshiro256::from_state([0, 0, 0, 0]).is_none());
        assert!(Xoshiro256::from_state([0, 0, 0, 1]).is_some());
    }

    #[test]
    fn state_roundtrip_resumes_stream() {
        let mut rng = Xoshiro256::seed_from_u64(99);
        for _ in 0..17 {
            let _ = rng.next_u64();
        }
        let snap = rng.state();
        let mut resumed = Xoshiro256::from_state(snap).unwrap();
        for _ in 0..32 {
            assert_eq!(rng.next_u64(), resumed.next_u64());
        }
    }

    #[test]
    fn splitmix64_known_value_zero_counter() {
        // splitmix64(seed=0) — first output is fully determined by the
        // algorithm constants and is a canonical regression vector.
        let mut x = 0u64;
        assert_eq!(splitmix64(&mut x), 0xE220_A839_7B1D_CDAF);
    }

    #[test]
    fn xoshiro_known_first_outputs_for_seed_zero() {
        // Regression vectors: generated from the canonical
        // splitmix64 + xoshiro256** construction with seed 0. Any
        // change here is either a bug or an intentional algorithm
        // change (which would be a breaking change to determinism).
        let mut rng = Xoshiro256::seed_from_u64(0);
        let v0 = rng.next_u64();
        let v1 = rng.next_u64();
        let v2 = rng.next_u64();
        let v3 = rng.next_u64();
        // First, ensure values are non-degenerate.
        assert_ne!(v0, 0);
        assert_ne!(v0, v1);
        assert_ne!(v1, v2);
        assert_ne!(v2, v3);
        // Lock in determinism: replaying from the same seed produces
        // the same sequence.
        let mut rng2 = Xoshiro256::seed_from_u64(0);
        assert_eq!(rng2.next_u64(), v0);
        assert_eq!(rng2.next_u64(), v1);
        assert_eq!(rng2.next_u64(), v2);
        assert_eq!(rng2.next_u64(), v3);
    }

    #[test]
    fn next_u32_takes_high_bits() {
        // The relationship `next_u32 = (next_u64 >> 32) as u32` must
        // hold against a fresh, identically-seeded generator.
        let mut a = Xoshiro256::seed_from_u64(7);
        let mut b = Xoshiro256::seed_from_u64(7);
        for _ in 0..64 {
            let hi = (a.next_u64() >> 32) as u32;
            assert_eq!(hi, b.next_u32());
        }
    }

    #[test]
    fn next_f64_bounds() {
        let mut rng = Xoshiro256::seed_from_u64(1);
        for _ in 0..10_000 {
            let f = rng.next_f64();
            assert!((0.0..1.0).contains(&f));
        }
    }

    #[test]
    fn fill_bytes_exact_chunks() {
        let mut a = Xoshiro256::seed_from_u64(123);
        let mut b = Xoshiro256::seed_from_u64(123);
        let mut buf = [0u8; 64];
        a.fill_bytes(&mut buf);
        for chunk in buf.chunks_exact(8) {
            let expected = b.next_u64().to_le_bytes();
            assert_eq!(chunk, expected);
        }
    }

    #[test]
    fn fill_bytes_partial_tail() {
        // Length 13 = one full 8-byte chunk plus 5 trailing bytes.
        let mut rng = Xoshiro256::seed_from_u64(2026);
        let mut buf = [0u8; 13];
        rng.fill_bytes(&mut buf);
        // Trivially: at least one byte must be nonzero — a 13-byte
        // all-zero output would be a 2^-104 freak event indicating a
        // bug, not chance.
        assert!(buf.iter().any(|&b| b != 0));
    }

    #[test]
    fn jump_produces_different_state() {
        let mut a = Xoshiro256::seed_from_u64(1);
        let mut b = a.clone();
        b.jump();
        assert_ne!(a.state(), b.state());
        // The two streams must not coincide on the first output.
        assert_ne!(a.next_u64(), b.next_u64());
    }

    #[test]
    fn long_jump_produces_different_state() {
        let mut a = Xoshiro256::seed_from_u64(1);
        let mut b = a.clone();
        b.long_jump();
        assert_ne!(a.state(), b.state());
        assert_ne!(a.next_u64(), b.next_u64());
    }

    #[test]
    fn jump_is_deterministic() {
        let mut a = Xoshiro256::seed_from_u64(5);
        let mut b = Xoshiro256::seed_from_u64(5);
        a.jump();
        b.jump();
        assert_eq!(a.state(), b.state());
    }

    // ------------------------------------------------------------
    // Bounded-range tests
    // ------------------------------------------------------------

    #[test]
    fn gen_range_u64_bounds() {
        let mut rng = Xoshiro256::seed_from_u64(1);
        for _ in 0..10_000 {
            let n = rng.gen_range_u64(100..200);
            assert!((100..200).contains(&n));
        }
    }

    #[test]
    fn gen_range_u64_single_value_at_top() {
        // [start, start+1) is a one-value half-open range; every draw
        // must equal start exactly.
        let mut rng = Xoshiro256::seed_from_u64(2);
        for _ in 0..1000 {
            assert_eq!(rng.gen_range_u64(7..8), 7);
        }
    }

    #[test]
    fn gen_range_inclusive_u64_die_roll() {
        // Classic 1d6: every draw must land on a face. Over enough
        // draws we'd expect to see all six faces appear.
        let mut rng = Xoshiro256::seed_from_u64(3);
        let mut faces = [0u32; 6];
        for _ in 0..10_000 {
            let d = rng.gen_range_inclusive_u64(1..=6);
            assert!((1..=6).contains(&d));
            faces[(d - 1) as usize] += 1;
        }
        for (i, &c) in faces.iter().enumerate() {
            assert!(c > 0, "face {} never appeared in 10000 rolls", i + 1);
        }
    }

    #[test]
    fn gen_range_inclusive_u64_single_value() {
        let mut rng = Xoshiro256::seed_from_u64(4);
        for _ in 0..1000 {
            assert_eq!(rng.gen_range_inclusive_u64(42..=42), 42);
        }
    }

    #[test]
    fn gen_range_inclusive_u64_full_width_uses_raw_draw() {
        // For 0..=u64::MAX the bounded helper would compute span=2^64
        // which overflows; the implementation special-cases this and
        // returns next_u64() unchanged. Verify by checking against a
        // freshly-seeded clone.
        let mut a = Xoshiro256::seed_from_u64(5);
        let mut b = Xoshiro256::seed_from_u64(5);
        for _ in 0..256 {
            assert_eq!(a.gen_range_inclusive_u64(0..=u64::MAX), b.next_u64());
        }
    }

    #[test]
    fn gen_range_u32_bounds() {
        let mut rng = Xoshiro256::seed_from_u64(6);
        for _ in 0..10_000 {
            let n = rng.gen_range_u32(0..256);
            assert!(n < 256);
        }
    }

    #[test]
    fn gen_range_inclusive_u32_full_width() {
        // For 0..=u32::MAX every value is in-range; just verify
        // bounds + that the call doesn't panic.
        let mut rng = Xoshiro256::seed_from_u64(7);
        for _ in 0..1000 {
            let _ = rng.gen_range_inclusive_u32(0..=u32::MAX);
        }
    }

    #[test]
    fn gen_range_i64_negative_range() {
        let mut rng = Xoshiro256::seed_from_u64(8);
        for _ in 0..10_000 {
            let n = rng.gen_range_i64(-100..-50);
            assert!((-100..-50).contains(&n));
        }
    }

    #[test]
    fn gen_range_i64_mixed_sign_range() {
        let mut rng = Xoshiro256::seed_from_u64(9);
        let mut saw_neg = false;
        let mut saw_pos = false;
        for _ in 0..10_000 {
            let n = rng.gen_range_i64(-100..100);
            assert!((-100..100).contains(&n));
            if n < 0 {
                saw_neg = true;
            }
            if n >= 0 {
                saw_pos = true;
            }
        }
        assert!(saw_neg && saw_pos);
    }

    #[test]
    fn gen_range_inclusive_i64_full_width_is_raw_draw() {
        // i64::MIN..=i64::MAX is the full i64 space (span = 2^64).
        // The implementation reinterprets next_u64 as i64.
        let mut a = Xoshiro256::seed_from_u64(10);
        let mut b = Xoshiro256::seed_from_u64(10);
        for _ in 0..256 {
            let from_range = a.gen_range_inclusive_i64(i64::MIN..=i64::MAX);
            let from_raw = b.next_u64() as i64;
            assert_eq!(from_range, from_raw);
        }
    }

    #[test]
    fn gen_range_i32_bounds() {
        let mut rng = Xoshiro256::seed_from_u64(11);
        for _ in 0..10_000 {
            let n = rng.gen_range_i32(-1000..1000);
            assert!((-1000..1000).contains(&n));
        }
    }

    #[test]
    fn gen_range_inclusive_i32_full_width() {
        let mut rng = Xoshiro256::seed_from_u64(12);
        for _ in 0..1000 {
            let _ = rng.gen_range_inclusive_i32(i32::MIN..=i32::MAX);
        }
    }

    #[test]
    fn gen_range_f64_bounds() {
        let mut rng = Xoshiro256::seed_from_u64(13);
        for _ in 0..10_000 {
            let x = rng.gen_range_f64(-1.0..1.0);
            assert!((-1.0..1.0).contains(&x));
        }
    }

    #[test]
    fn gen_range_f64_positive_range() {
        let mut rng = Xoshiro256::seed_from_u64(14);
        for _ in 0..10_000 {
            let x = rng.gen_range_f64(10.0..20.0);
            assert!((10.0..20.0).contains(&x));
        }
    }

    #[test]
    #[should_panic(expected = "empty range")]
    fn gen_range_u64_panics_on_empty() {
        let mut rng = Xoshiro256::seed_from_u64(1);
        let _ = rng.gen_range_u64(10..10);
    }

    #[test]
    #[should_panic(expected = "empty range")]
    #[allow(clippy::reversed_empty_ranges)]
    fn gen_range_u64_panics_on_reverse() {
        let mut rng = Xoshiro256::seed_from_u64(1);
        let _ = rng.gen_range_u64(10..5);
    }

    #[test]
    #[should_panic(expected = "empty range")]
    #[allow(clippy::reversed_empty_ranges)]
    fn gen_range_inclusive_u64_panics_on_reverse() {
        let mut rng = Xoshiro256::seed_from_u64(1);
        let _ = rng.gen_range_inclusive_u64(10..=5);
    }

    #[test]
    #[should_panic(expected = "empty range")]
    #[allow(clippy::reversed_empty_ranges)]
    fn gen_range_i64_panics_on_reverse() {
        let mut rng = Xoshiro256::seed_from_u64(1);
        let _ = rng.gen_range_i64(5..-5);
    }

    #[test]
    #[should_panic(expected = "non-finite")]
    fn gen_range_f64_panics_on_nan() {
        let mut rng = Xoshiro256::seed_from_u64(1);
        let _ = rng.gen_range_f64(f64::NAN..1.0);
    }

    #[test]
    #[should_panic(expected = "non-finite")]
    fn gen_range_f64_panics_on_infinity() {
        let mut rng = Xoshiro256::seed_from_u64(1);
        let _ = rng.gen_range_f64(0.0..f64::INFINITY);
    }

    #[test]
    #[should_panic(expected = "empty range")]
    fn gen_range_f64_panics_on_reverse() {
        let mut rng = Xoshiro256::seed_from_u64(1);
        let _ = rng.gen_range_f64(1.0..0.0);
    }

    #[test]
    #[should_panic(expected = "overflows to infinity")]
    fn gen_range_f64_panics_when_span_overflows() {
        // Both bounds are finite, but their difference is not. Without
        // this guard the call would return +inf or NaN for any nonzero
        // u and -inf or NaN for u == 0.
        let mut rng = Xoshiro256::seed_from_u64(1);
        let _ = rng.gen_range_f64(-f64::MAX..f64::MAX);
    }

    #[test]
    fn gen_range_f64_output_is_finite_for_finite_span() {
        let mut rng = Xoshiro256::seed_from_u64(0xF1_F1_F1);
        for _ in 0..10_000 {
            let x = rng.gen_range_f64(-1e100..1e100);
            assert!(x.is_finite(), "got non-finite {x}");
            assert!((-1e100..1e100).contains(&x));
        }
    }

    #[test]
    fn gen_range_is_deterministic_under_same_seed() {
        // Replay invariant: same seed + same sequence of calls => same
        // outputs. This is the core determinism guarantee.
        let mut a = Xoshiro256::seed_from_u64(99);
        let mut b = Xoshiro256::seed_from_u64(99);
        for _ in 0..1000 {
            assert_eq!(a.gen_range_u64(0..1000), b.gen_range_u64(0..1000));
        }
    }

    #[test]
    fn gen_range_uniformity_chi_squared() {
        // 100 000 draws over a range of 100 buckets. Expected per
        // bucket: 1000. Chi-squared critical value (99 d.f.,
        // alpha=0.001) is ~149; we use 250 to keep flake rate
        // negligible on slow CI.
        let mut rng = Xoshiro256::seed_from_u64(0xC0DE_F00D);
        let mut counts = [0u32; 100];
        for _ in 0..100_000 {
            let v = rng.gen_range_u32(0..100);
            counts[v as usize] += 1;
        }
        let expected = 100_000.0 / 100.0;
        let chi: f64 = counts
            .iter()
            .map(|&c| {
                let diff = c as f64 - expected;
                diff * diff / expected
            })
            .sum();
        assert!(
            chi < 250.0,
            "chi-squared {chi} too high — bounded-range output is biased"
        );
    }

    #[test]
    fn gen_range_die_roll_uniformity() {
        // 600 000 die rolls. Expected count per face: 100 000.
        // Chi-squared on 5 d.f., alpha=0.001 is ~21; cap at 50.
        // Specifically targets the d6 case to catch modulo bias if
        // anyone replaces the rejection sampling with `% 6`.
        let mut rng = Xoshiro256::seed_from_u64(0xD1CE_D011);
        let mut faces = [0u32; 6];
        for _ in 0..600_000 {
            let d = rng.gen_range_inclusive_u32(1..=6);
            faces[(d - 1) as usize] += 1;
        }
        let expected = 100_000.0;
        let chi: f64 = faces
            .iter()
            .map(|&c| {
                let diff = c as f64 - expected;
                diff * diff / expected
            })
            .sum();
        assert!(chi < 50.0, "d6 uniformity chi-squared {chi} too high");
    }
}