synapse
A component of the AienOS ecosystem.
Brought by drakestapleton
synapse / crates / synapse-bench / src / rng.rs
| 1 | //! Deterministic SplitMix64 generator used for reproducible workloads, sample |
| 2 | //! selection, and cell ordering (no external `rand` dependency). |
| 3 | |
| 4 | /// SplitMix64 pseudo-random generator (Steele, Lea, and Flood, 2014). |
| 5 | /// The same seed always yields the same sequence on every host. |
| 6 | #[derive(Debug, Clone)] |
| 7 | pub struct SplitMix64 { |
| 8 | state: u64, |
| 9 | } |
| 10 | |
| 11 | impl SplitMix64 { |
| 12 | pub fn new(seed: u64) -> Self { |
| 13 | Self { state: seed } |
| 14 | } |
| 15 | |
| 16 | /// Derive an independent stream from `seed` and an ordered list of labels |
| 17 | /// (for example `[rep, agents, agent, index]`). |
| 18 | pub fn derive(seed: u64, labels: &[u64]) -> Self { |
| 19 | let mut mixed = Self::new(seed).next_u64(); |
| 20 | for &label in labels { |
| 21 | mixed = Self::new(mixed ^ label.wrapping_mul(0x9E37_79B9_7F4A_7C15)).next_u64(); |
| 22 | } |
| 23 | Self::new(mixed) |
| 24 | } |
| 25 | |
| 26 | pub fn next_u64(&mut self) -> u64 { |
| 27 | self.state = self.state.wrapping_add(0x9E37_79B9_7F4A_7C15); |
| 28 | let mut z = self.state; |
| 29 | z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); |
| 30 | z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); |
| 31 | z ^ (z >> 31) |
| 32 | } |
| 33 | |
| 34 | /// Uniform index in `0..n` via a 128-bit multiply-shift. Returns 0 when `n == 0`. |
| 35 | pub fn below(&mut self, n: usize) -> usize { |
| 36 | if n == 0 { |
| 37 | return 0; |
| 38 | } |
| 39 | ((u128::from(self.next_u64()) * n as u128) >> 64) as usize |
| 40 | } |
| 41 | |
| 42 | /// Inclusive range `lo..=hi` (returns `lo` when `hi < lo`). |
| 43 | pub fn range(&mut self, lo: usize, hi: usize) -> usize { |
| 44 | if hi <= lo { |
| 45 | return lo; |
| 46 | } |
| 47 | lo + self.below(hi - lo + 1) |
| 48 | } |
| 49 | |
| 50 | /// Fisher-Yates shuffle. |
| 51 | pub fn shuffle<T>(&mut self, items: &mut [T]) { |
| 52 | for i in (1..items.len()).rev() { |
| 53 | let j = self.below(i + 1); |
| 54 | items.swap(i, j); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /// Sixteen lowercase hex characters from the next output. |
| 59 | pub fn hex16(&mut self) -> String { |
| 60 | format!("{:016x}", self.next_u64()) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | #[cfg(test)] |
| 65 | mod tests { |
| 66 | use super::*; |
| 67 | |
| 68 | #[test] |
| 69 | fn same_seed_same_sequence_and_derive_is_label_sensitive() { |
| 70 | let mut a = SplitMix64::new(42); |
| 71 | let mut b = SplitMix64::new(42); |
| 72 | for _ in 0..16 { |
| 73 | assert_eq!(a.next_u64(), b.next_u64()); |
| 74 | } |
| 75 | let x = SplitMix64::derive(7, &[1, 2, 3]).next_u64(); |
| 76 | let y = SplitMix64::derive(7, &[1, 2, 3]).next_u64(); |
| 77 | let z = SplitMix64::derive(7, &[1, 2, 4]).next_u64(); |
| 78 | assert_eq!(x, y); |
| 79 | assert_ne!(x, z); |
| 80 | } |
| 81 | |
| 82 | #[test] |
| 83 | fn below_stays_in_bounds_and_shuffle_is_permutation() { |
| 84 | let mut g = SplitMix64::new(1); |
| 85 | for n in 1..50 { |
| 86 | assert!(g.below(n) < n); |
| 87 | } |
| 88 | assert_eq!(g.below(0), 0); |
| 89 | let mut v: Vec<u32> = (0..100).collect(); |
| 90 | g.shuffle(&mut v); |
| 91 | let mut sorted = v.clone(); |
| 92 | sorted.sort_unstable(); |
| 93 | assert_eq!(sorted, (0..100).collect::<Vec<u32>>()); |
| 94 | } |
| 95 | } |