rust/lfprng/src/lib.rs
author unC0Rr
Wed, 04 Jan 2023 10:37:12 +0100
branchtransitional_engine
changeset 15902 9b73594ac986
parent 15115 de32299de704
permissions -rw-r--r--
Update lfprng for better distribution, make it conform 'rand' package traits
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15902
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
     1
use rand::{Error, RngCore, SeedableRng};
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
     2
13886
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
     3
pub struct LaggedFibonacciPRNG {
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
     4
    circular_buffer: [u32; 64],
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
     5
    index: usize,
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
     6
}
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
     7
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
     8
impl LaggedFibonacciPRNG {
14027
cef0c685fda8 make useful stuff public
alfadur
parents: 13935
diff changeset
     9
    pub fn new(init_values: &[u8]) -> Self {
15902
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    10
        let mut buf = [0xa98765; 64];
13886
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    11
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    12
        for i in 0..std::cmp::min(init_values.len(), 54) {
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    13
            buf[i] = init_values[i] as u32;
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    14
        }
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    15
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    16
        let mut prng = Self {
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    17
            circular_buffer: buf,
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    18
            index: 54,
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    19
        };
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    20
15115
de32299de704 Add 'discard' method for lfprng
unc0rr
parents: 14152
diff changeset
    21
        prng.discard(2048);
13886
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    22
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    23
        prng
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    24
    }
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    25
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    26
    #[inline]
15115
de32299de704 Add 'discard' method for lfprng
unc0rr
parents: 14152
diff changeset
    27
    pub fn discard(&mut self, count: usize) {
de32299de704 Add 'discard' method for lfprng
unc0rr
parents: 14152
diff changeset
    28
        for _i in 0..count {
de32299de704 Add 'discard' method for lfprng
unc0rr
parents: 14152
diff changeset
    29
            self.get_next();
de32299de704 Add 'discard' method for lfprng
unc0rr
parents: 14152
diff changeset
    30
        }
de32299de704 Add 'discard' method for lfprng
unc0rr
parents: 14152
diff changeset
    31
    }
de32299de704 Add 'discard' method for lfprng
unc0rr
parents: 14152
diff changeset
    32
de32299de704 Add 'discard' method for lfprng
unc0rr
parents: 14152
diff changeset
    33
    #[inline]
14029
259175ab7e8c hide get_next back
alfadur
parents: 14027
diff changeset
    34
    fn get_next(&mut self) -> u32 {
15902
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    35
        const PRIME_NUM: u32 = 2147483629;
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    36
13886
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    37
        self.index = (self.index + 1) & 0x3f;
15902
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    38
        let next_value = self.circular_buffer[(self.index + 40) & 0x3f]
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    39
            + self.circular_buffer[(self.index + 9) & 0x3f];
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    40
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    41
        self.circular_buffer[self.index] = if next_value > PRIME_NUM {
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    42
            next_value - PRIME_NUM
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    43
        } else {
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    44
            next_value
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    45
        };
13886
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    46
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    47
        self.circular_buffer[self.index]
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    48
    }
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    49
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    50
    #[inline]
14027
cef0c685fda8 make useful stuff public
alfadur
parents: 13935
diff changeset
    51
    pub fn get_random(&mut self, modulo: u32) -> u32 {
13886
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    52
        self.get_next();
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    53
        self.get_next() % modulo
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    54
    }
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    55
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    56
    #[inline]
14027
cef0c685fda8 make useful stuff public
alfadur
parents: 13935
diff changeset
    57
    pub fn add_randomness(&mut self, value: u32) {
13886
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    58
        self.index = (self.index + 1) & 0x3f;
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    59
        self.circular_buffer[self.index] ^= value;
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    60
    }
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    61
}
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    62
13904
3f0576157749 Implement Iterator trait for LaggedFibonacciPRNG
unc0rr
parents: 13886
diff changeset
    63
impl Iterator for LaggedFibonacciPRNG {
3f0576157749 Implement Iterator trait for LaggedFibonacciPRNG
unc0rr
parents: 13886
diff changeset
    64
    type Item = u32;
3f0576157749 Implement Iterator trait for LaggedFibonacciPRNG
unc0rr
parents: 13886
diff changeset
    65
13935
75eaf7c71789 Introduce integral-geometry crate, implement LinePoints iterator
unc0rr
parents: 13904
diff changeset
    66
    fn next(&mut self) -> Option<Self::Item> {
13904
3f0576157749 Implement Iterator trait for LaggedFibonacciPRNG
unc0rr
parents: 13886
diff changeset
    67
        self.get_next();
3f0576157749 Implement Iterator trait for LaggedFibonacciPRNG
unc0rr
parents: 13886
diff changeset
    68
        Some(self.get_next())
3f0576157749 Implement Iterator trait for LaggedFibonacciPRNG
unc0rr
parents: 13886
diff changeset
    69
    }
3f0576157749 Implement Iterator trait for LaggedFibonacciPRNG
unc0rr
parents: 13886
diff changeset
    70
}
3f0576157749 Implement Iterator trait for LaggedFibonacciPRNG
unc0rr
parents: 13886
diff changeset
    71
15902
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    72
impl RngCore for LaggedFibonacciPRNG {
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    73
    fn next_u32(&mut self) -> u32 {
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    74
        self.get_next().wrapping_add(self.get_next())
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    75
    }
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    76
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    77
    fn next_u64(&mut self) -> u64 {
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    78
        ((self.next_u32() as u64) << 32) | self.next_u32() as u64
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    79
    }
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    80
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    81
    fn fill_bytes(&mut self, dest: &mut [u8]) {
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    82
        dest.iter_mut().for_each(|x| *x = self.next_u32() as u8);
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    83
    }
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    84
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    85
    fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    86
        Ok(self.fill_bytes(dest))
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    87
    }
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    88
}
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    89
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    90
impl SeedableRng for LaggedFibonacciPRNG {
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    91
    type Seed = [u8; 32];
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    92
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    93
    fn from_seed(seed: Self::Seed) -> Self {
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    94
        LaggedFibonacciPRNG::new(&seed)
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    95
    }
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    96
}
9b73594ac986 Update lfprng for better distribution, make it conform 'rand' package traits
unC0Rr
parents: 15115
diff changeset
    97
13886
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    98
#[cfg(test)]
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
    99
#[test]
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
   100
fn compatibility() {
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
   101
    let mut prng = LaggedFibonacciPRNG::new("{052e2aee-ce41-4720-97bd-559a413bf866}".as_bytes());
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
   102
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
   103
    assert_eq!(prng.get_random(1000), 418);
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
   104
    assert_eq!(prng.get_random(1000000), 554064);
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
   105
    assert_eq!(prng.get_random(0xffffffff), 239515837);
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
   106
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
   107
    prng.add_randomness(123);
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
   108
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
   109
    for i in 0..=100000 {
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
   110
        prng.get_random(2);
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
   111
    }
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
   112
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
   113
    assert_eq!(prng.get_random(0xffffffff), 525333582);
b6c35ac1c5ba Implement lagged Fibonacci PRNG in rust, compatible with hwengine
unc0rr
parents:
diff changeset
   114
}