rand/rngs/
xoshiro128plusplus.rs1use core::convert::Infallible;
10use rand_core::{SeedableRng, TryRng, utils};
11#[cfg(feature = "serde")]
12use serde::{Deserialize, Serialize};
13
14#[derive(#[automatically_derived]
impl ::core::fmt::Debug for Xoshiro128PlusPlus {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field1_finish(f,
"Xoshiro128PlusPlus", "s", &&self.s)
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for Xoshiro128PlusPlus {
#[inline]
fn clone(&self) -> Xoshiro128PlusPlus {
Xoshiro128PlusPlus { s: ::core::clone::Clone::clone(&self.s) }
}
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for Xoshiro128PlusPlus {
#[inline]
fn eq(&self, other: &Xoshiro128PlusPlus) -> bool { self.s == other.s }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for Xoshiro128PlusPlus {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<[u32; 4]>;
}
}Eq)]
23#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
24pub struct Xoshiro128PlusPlus {
25 s: [u32; 4],
26}
27
28impl SeedableRng for Xoshiro128PlusPlus {
29 type Seed = [u8; 16];
30
31 #[inline]
34 fn from_seed(seed: [u8; 16]) -> Xoshiro128PlusPlus {
35 let state = utils::read_words(&seed);
36 if state.iter().all(|&x| x == 0) {
39 return Self::seed_from_u64(0);
40 }
41 Xoshiro128PlusPlus { s: state }
42 }
43
44 #[inline]
48 fn seed_from_u64(mut state: u64) -> Self {
49 const PHI: u64 = 0x9e3779b97f4a7c15;
50 let mut s = [0; 4];
51 for i in s.chunks_exact_mut(2) {
52 state = state.wrapping_add(PHI);
53 let mut z = state;
54 z = (z ^ (z >> 30)).wrapping_mul(0xbf58476d1ce4e5b9);
55 z = (z ^ (z >> 27)).wrapping_mul(0x94d049bb133111eb);
56 z = z ^ (z >> 31);
57 i[0] = z as u32;
58 i[1] = (z >> 32) as u32;
59 }
60 if true {
match (&s, &[0; 4]) {
(left_val, right_val) => {
if *left_val == *right_val {
let kind = ::core::panicking::AssertKind::Ne;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_ne!(s, [0; 4]);
63 Xoshiro128PlusPlus { s }
64 }
65}
66
67impl TryRng for Xoshiro128PlusPlus {
68 type Error = Infallible;
69
70 #[inline]
71 fn try_next_u32(&mut self) -> Result<u32, Infallible> {
72 let res = self.s[0]
73 .wrapping_add(self.s[3])
74 .rotate_left(7)
75 .wrapping_add(self.s[0]);
76
77 let t = self.s[1] << 9;
78
79 self.s[2] ^= self.s[0];
80 self.s[3] ^= self.s[1];
81 self.s[1] ^= self.s[2];
82 self.s[0] ^= self.s[3];
83
84 self.s[2] ^= t;
85
86 self.s[3] = self.s[3].rotate_left(11);
87
88 Ok(res)
89 }
90
91 #[inline]
92 fn try_next_u64(&mut self) -> Result<u64, Infallible> {
93 utils::next_u64_via_u32(self)
94 }
95
96 #[inline]
97 fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Infallible> {
98 utils::fill_bytes_via_next_word(dst, || self.try_next_u32())
99 }
100}
101
102#[cfg(test)]
103mod tests {
104 use super::Xoshiro128PlusPlus;
105 use rand_core::{Rng, SeedableRng};
106
107 #[test]
108 fn reference() {
109 let mut rng =
110 Xoshiro128PlusPlus::from_seed([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]);
111 let expected = [
114 641, 1573767, 3222811527, 3517856514, 836907274, 4247214768, 3867114732, 1355841295,
115 495546011, 621204420,
116 ];
117 for &e in &expected {
118 assert_eq!(rng.next_u32(), e);
119 }
120 }
121
122 #[test]
123 fn stable_seed_from_u64_and_from_seed() {
124 let mut rng = Xoshiro128PlusPlus::seed_from_u64(0);
127 let mut rng_from_seed_0 = Xoshiro128PlusPlus::from_seed([0; 16]);
129 let expected = [
130 1179900579, 1938959192, 3089844957, 3657088315, 1015453891, 479942911, 3433842246,
131 669252886, 3985671746, 2737205563,
132 ];
133 for &e in &expected {
134 assert_eq!(rng.next_u32(), e);
135 assert_eq!(rng_from_seed_0.next_u32(), e);
136 }
137 }
138}