|
50 | 50 |
|
51 | 51 | Any feedback is very welcome. |
52 | 52 |
|
53 | | - http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html |
| 53 | + <http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html> |
54 | 54 |
|
55 | 55 | email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) |
56 | 56 | */ |
|
59 | 59 | // https://gist.github.com/coolreader18/b56d510f1b0551d2954d74ad289f7d2e |
60 | 60 |
|
61 | 61 | /* Period parameters */ |
62 | | -const N: usize = 624; |
| 62 | +pub const N: usize = 624; |
63 | 63 | const M: usize = 397; |
64 | 64 | const MATRIX_A: u32 = 0x9908b0dfu32; /* constant vector a */ |
65 | 65 | const UPPER_MASK: u32 = 0x80000000u32; /* most significant w-r bits */ |
66 | 66 | const LOWER_MASK: u32 = 0x7fffffffu32; /* least significant r bits */ |
67 | 67 |
|
68 | | -/// rand::Rng instance implementing the mt19937 mersenne twister algorithm |
| 68 | +/// `rand::Rng` instance implementing the mt19937 mersenne twister algorithm |
69 | 69 | pub struct MT19937 { |
70 | 70 | mt: [u32; N], /* the array for the state vector */ |
71 | 71 | mti: usize, /* mti==N+1 means mt[N] is not initialized */ |
@@ -200,12 +200,35 @@ impl MT19937 { |
200 | 200 |
|
201 | 201 | y |
202 | 202 | } |
| 203 | + |
| 204 | + /// Returns the internal state of this rng. |
| 205 | + pub fn get_state(&self) -> &[u32; N] { |
| 206 | + &self.mt |
| 207 | + } |
| 208 | + |
| 209 | + /// Returns the internal index of this rng. |
| 210 | + pub fn get_index(&self) -> usize { |
| 211 | + self.mti |
| 212 | + } |
| 213 | + |
| 214 | + /// Set the internal state of this rng. |
| 215 | + pub fn set_state(&mut self, mt: &[u32; N]) { |
| 216 | + self.mt = *mt; |
| 217 | + } |
| 218 | + |
| 219 | + /// Set the internal index of this rng. |
| 220 | + /// |
| 221 | + /// Must be less than or equal to [N]. |
| 222 | + pub fn set_index(&mut self, mti: usize) { |
| 223 | + assert!(mti <= N); |
| 224 | + self.mti = mti; |
| 225 | + } |
203 | 226 | } |
204 | 227 |
|
205 | | -/** generates a random number on [0,1) with 53-bit resolution*/ |
| 228 | +/** generates a random number on `[0,1)` with 53-bit resolution*/ |
206 | 229 | /// |
207 | 230 | /// This generates a float with the same algorithm that CPython uses; calling |
208 | | -/// it with an [`MT19937`](struct.MT19937.html) with a given seed returns the same as it would in CPython. |
| 231 | +/// it with an [`MT19937`] with a given seed returns the same as it would in CPython. |
209 | 232 | /// |
210 | 233 | /// e.g.: |
211 | 234 | /// ``` |
@@ -248,7 +271,7 @@ impl rand_core::RngCore for MT19937 { |
248 | 271 | } |
249 | 272 | } |
250 | 273 |
|
251 | | -/// Seed for <MT19937 as rand_core::SeedableRng> |
| 274 | +/// Seed for <code><[MT19937] as [rand_core::SeedableRng]>::from_seed()</code> |
252 | 275 | /// |
253 | 276 | /// Very big seed, but this is the size that CPython uses as well |
254 | 277 | #[derive(Clone, Copy)] |
|
0 commit comments