1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
html_root_url = "https://docs.rs/rand_xorshift/0.3.0")]
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#![no_std]
use core::num::Wrapping as w;
use core::fmt;
use rand_core::{RngCore, SeedableRng, Error, impls, le};
#[cfg(feature="serde1")] use serde::{Serialize, Deserialize};
#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature="serde1", derive(Serialize,Deserialize))]
pub struct XorShiftRng {
x: w<u32>,
y: w<u32>,
z: w<u32>,
w: w<u32>,
}
impl fmt::Debug for XorShiftRng {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "XorShiftRng {{}}")
}
}
impl RngCore for XorShiftRng {
#[inline]
fn next_u32(&mut self) -> u32 {
let x = self.x;
let t = x ^ (x << 11);
self.x = self.y;
self.y = self.z;
self.z = self.w;
let w_ = self.w;
self.w = w_ ^ (w_ >> 19) ^ (t ^ (t >> 8));
self.w.0
}
#[inline]
fn next_u64(&mut self) -> u64 {
impls::next_u64_via_u32(self)
}
#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
impls::fill_bytes_via_next(self, dest)
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.fill_bytes(dest);
Ok(())
}
}
impl SeedableRng for XorShiftRng {
type Seed = [u8; 16];
fn from_seed(seed: Self::Seed) -> Self {
let mut seed_u32 = [0u32; 4];
le::read_u32_into(&seed, &mut seed_u32);
if seed_u32.iter().all(|&x| x == 0) {
seed_u32 = [0xBAD_5EED, 0xBAD_5EED, 0xBAD_5EED, 0xBAD_5EED];
}
XorShiftRng {
x: w(seed_u32[0]),
y: w(seed_u32[1]),
z: w(seed_u32[2]),
w: w(seed_u32[3]),
}
}
fn from_rng<R: RngCore>(mut rng: R) -> Result<Self, Error> {
let mut b = [0u8; 16];
loop {
rng.try_fill_bytes(&mut b[..])?;
if !b.iter().all(|&x| x == 0) {
break;
}
}
Ok(XorShiftRng {
x: w(u32::from_le_bytes([b[0], b[1], b[2], b[3]])),
y: w(u32::from_le_bytes([b[4], b[5], b[6], b[7]])),
z: w(u32::from_le_bytes([b[8], b[9], b[10], b[11]])),
w: w(u32::from_le_bytes([b[12], b[13], b[14], b[15]])),
})
}
}