Skip to content

Commit 8fa8b22

Browse files
committed
Improve docs and add state accessors
1 parent 0551b99 commit 8fa8b22

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mt19937"
3-
version = "3.0.0"
3+
version = "3.1.0"
44
authors = ["Noa <coolreader18@gmail.com>", "RustPython Team"]
55
edition = "2021"
66
license-file = "LICENSE"

src/lib.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
5151
Any feedback is very welcome.
5252
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>
5454
5555
email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
5656
*/
@@ -59,13 +59,13 @@
5959
// https://gist.github.com/coolreader18/b56d510f1b0551d2954d74ad289f7d2e
6060

6161
/* Period parameters */
62-
const N: usize = 624;
62+
pub const N: usize = 624;
6363
const M: usize = 397;
6464
const MATRIX_A: u32 = 0x9908b0dfu32; /* constant vector a */
6565
const UPPER_MASK: u32 = 0x80000000u32; /* most significant w-r bits */
6666
const LOWER_MASK: u32 = 0x7fffffffu32; /* least significant r bits */
6767

68-
/// rand::Rng instance implementing the mt19937 mersenne twister algorithm
68+
/// `rand::Rng` instance implementing the mt19937 mersenne twister algorithm
6969
pub struct MT19937 {
7070
mt: [u32; N], /* the array for the state vector */
7171
mti: usize, /* mti==N+1 means mt[N] is not initialized */
@@ -200,12 +200,35 @@ impl MT19937 {
200200

201201
y
202202
}
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+
}
203226
}
204227

205-
/** generates a random number on [0,1) with 53-bit resolution*/
228+
/** generates a random number on `[0,1)` with 53-bit resolution*/
206229
///
207230
/// 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.
209232
///
210233
/// e.g.:
211234
/// ```
@@ -248,7 +271,7 @@ impl rand_core::RngCore for MT19937 {
248271
}
249272
}
250273

251-
/// Seed for <MT19937 as rand_core::SeedableRng>
274+
/// Seed for <code>&lt;[MT19937] as [rand_core::SeedableRng]&gt;::from_seed()</code>
252275
///
253276
/// Very big seed, but this is the size that CPython uses as well
254277
#[derive(Clone, Copy)]

0 commit comments

Comments
 (0)