// SPDX-License-Identifier: MIT //! Python's `random.Random(seed).getrandbits(bits)`, for `POKEY::power_on(seed)`. //! //! Not a module of the reference: it is the part of Python's standard library that //! pokey-python's `power_on(seed)` uses (Rust-forced: Rust has no `random.Random`). For a seed //! to give the same chip state on both cores, the generator and its seeding must be Python's: //! MT19937 (M. Matsumoto and T. Nishimura, "Mersenne Twister", ACM TOMACS 8(1), 1998, the //! algorithm as that paper and its `init_by_array` describe it), seeded the way CPython 3.11 //! and PyPy 3.11 seed it from an integer: the absolute value split into 32-bit words, least //! significant first (one word 0 for the seed 0), passed to `init_by_array`; `getrandbits(k)` //! for k <= 32 is the next tempered word shifted right by 32 - k. Written from that //! description; the lockstep (rung 2) compares seeded power-ons field by field. const N: usize = 624; const M: usize = 397; const MATRIX_A: u32 = 0x9908_b0df; const UPPER_MASK: u32 = 0x8000_0000; const LOWER_MASK: u32 = 0x7fff_ffff; pub(crate) struct PyRandom { mt: [u32; N], index: usize, } impl PyRandom { /// `random.Random(seed)` for a non-negative integer seed. pub(crate) fn new(seed: u64) -> Self { let mut key = vec![seed as u32]; if seed >> 32 != 0 { key.push((seed >> 32) as u32); } let mut r = PyRandom { mt: [0; N], index: N, }; r.init_by_array(&key); r } fn init_genrand(&mut self, s: u32) { self.mt[0] = s; for i in 1..N { let prev = self.mt[i - 1]; self.mt[i] = 1_812_433_253u32 .wrapping_mul(prev ^ (prev >> 30)) .wrapping_add(i as u32); } self.index = N; } fn init_by_array(&mut self, key: &[u32]) { self.init_genrand(19_650_218); let mut i = 1usize; let mut j = 0usize; for _ in 0..N.max(key.len()) { let prev = self.mt[i - 1]; self.mt[i] = (self.mt[i] ^ (prev ^ (prev >> 30)).wrapping_mul(1_664_525)) .wrapping_add(key[j]) .wrapping_add(j as u32); i += 1; j += 1; if i >= N { self.mt[0] = self.mt[N - 1]; i = 1; } if j >= key.len() { j = 0; } } for _ in 0..N - 1 { let prev = self.mt[i - 1]; self.mt[i] = (self.mt[i] ^ (prev ^ (prev >> 30)).wrapping_mul(1_566_083_941)) .wrapping_sub(i as u32); i += 1; if i >= N { self.mt[0] = self.mt[N - 1]; i = 1; } } self.mt[0] = 0x8000_0000; } fn genrand_u32(&mut self) -> u32 { if self.index >= N { for kk in 0..N { let y = (self.mt[kk] & UPPER_MASK) | (self.mt[(kk + 1) % N] & LOWER_MASK); let mag = if y & 1 != 0 { MATRIX_A } else { 0 }; self.mt[kk] = self.mt[(kk + M) % N] ^ (y >> 1) ^ mag; } self.index = 0; } let mut y = self.mt[self.index]; self.index += 1; y ^= y >> 11; y ^= (y << 7) & 0x9d2c_5680; y ^= (y << 15) & 0xefc6_0000; y ^= y >> 18; y } /// `getrandbits(bits)` for 1 <= bits <= 32. pub(crate) fn getrandbits(&mut self, bits: u32) -> u32 { self.genrand_u32() >> (32 - bits) } } #[cfg(test)] mod tests { use super::PyRandom; #[test] fn matches_the_published_first_output_of_init_by_array() { // The MT19937 reference output for init_by_array({0x123, 0x234, 0x345, 0x456}): // the first genrand_int32() is 1067595299 (mt19937ar.out, Matsumoto and Nishimura). let mut r = PyRandom { mt: [0; super::N], index: super::N, }; r.init_by_array(&[0x123, 0x234, 0x345, 0x456]); assert_eq!(r.genrand_u32(), 1_067_595_299); } }