use rand_core::{OsRng, RngCore};
pub struct IndexLfsr {
initial: u32,
lfsr: u32,
mask: u32,
}
impl IndexLfsr {
pub fn random_index() -> u32 {
const LFSR_MAX: u32 = 0xffffff; loop {
let i = OsRng.next_u32() & LFSR_MAX;
if i > 0 {
return i;
}
}
}
pub fn next(&mut self) -> u32 {
const LFSR_POLY: u32 = 0xd80000; let value = self.lfsr - 1; self.lfsr = (self.lfsr >> 1) ^ ((0u32.wrapping_sub(self.lfsr & 1u32)) & LFSR_POLY);
assert!(self.lfsr != self.initial, "Too many peers created");
value ^ self.mask
}
}
impl Default for IndexLfsr {
fn default() -> Self {
let seed = Self::random_index();
IndexLfsr {
initial: seed,
lfsr: seed,
mask: Self::random_index(),
}
}
}