Skip to content

Commit f154ba2

Browse files
committed
Use MCG in doc example
1 parent 235e8bd commit f154ba2

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

src/utils.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,50 +36,48 @@
3636
//!
3737
//! ## Example
3838
//!
39-
//! We demonstrate a simple "step RNG":
39+
//! We demonstrate a simple multiplicative congruential generator (MCG), taken
40+
//! from M.E. O'Neill's blog post
41+
//! [Does It Beat the Minimal Standard?](https://www.pcg-random.org/posts/does-it-beat-the-minimal-standard.html).
4042
//! ```
4143
//! use rand_core::{RngCore, SeedableRng, utils};
4244
//!
43-
//! pub struct Step32Rng {
44-
//! state: u32
45-
//! }
45+
//! pub struct Mcg128(u128);
4646
//!
47-
//! impl SeedableRng for Step32Rng {
48-
//! type Seed = [u8; 4];
47+
//! impl SeedableRng for Mcg128 {
48+
//! type Seed = [u8; 16];
4949
//!
5050
//! #[inline]
5151
//! fn from_seed(seed: Self::Seed) -> Self {
5252
//! // Always use little-endian byte order to ensure portable results
53-
//! let state = u32::from_le_bytes(seed);
54-
//! Self { state }
53+
//! Self(u128::from_le_bytes(seed))
5554
//! }
5655
//! }
5756
//!
58-
//! impl RngCore for Step32Rng {
57+
//! impl RngCore for Mcg128 {
5958
//! #[inline]
6059
//! fn next_u32(&mut self) -> u32 {
61-
//! let val = self.state;
62-
//! self.state = val + 1;
63-
//! val
60+
//! (self.next_u64() >> 32) as u32
6461
//! }
6562
//!
6663
//! #[inline]
6764
//! fn next_u64(&mut self) -> u64 {
68-
//! utils::next_u64_via_u32(self)
65+
//! self.0 = self.0.wrapping_mul(0x0fc94e3bf4e9ab32866458cd56f5e605);
66+
//! (self.0 >> 64) as u64
6967
//! }
7068
//!
7169
//! #[inline]
7270
//! fn fill_bytes(&mut self, dst: &mut [u8]) {
73-
//! utils::fill_bytes_via_next_word(dst, || self.next_u32());
71+
//! utils::fill_bytes_via_next_word(dst, || self.next_u64());
7472
//! }
7573
//! }
76-
//!
77-
//! # let mut rng = Step32Rng::seed_from_u64(42);
78-
//! # assert_eq!(rng.next_u32(), 0x7ba1_8fa4);
79-
//! # assert_eq!(rng.next_u64(), 0x7ba1_8fa6_7ba1_8fa5);
74+
//! #
75+
//! # let mut rng = Mcg128::seed_from_u64(42);
76+
//! # assert_eq!(rng.next_u32(), 3443086493);
77+
//! # assert_eq!(rng.next_u64(), 3462997187007721903);
8078
//! # let mut buf = [0u8; 5];
8179
//! # rng.fill_bytes(&mut buf);
82-
//! # assert_eq!(buf, [0xa7, 0x8f, 0xa1, 0x7b, 0xa8]);
80+
//! # assert_eq!(buf, [154, 23, 43, 68, 75]);
8381
//! ```
8482
8583
use crate::RngCore;

0 commit comments

Comments
 (0)