pub struct PseudoRng { /* private fields */ }Expand description
Linear congruential generator with the parameters used by glibc’s
non-secure random() family. Sufficient for ring dispatch (the only
caller); not cryptographically strong.
§Examples
use dynomite::hashkit::PseudoRng;
let mut rng = PseudoRng::from_seed(42);
let value = rng.next_u32();
// Same seed reproduces the same stream.
let mut rng2 = PseudoRng::from_seed(42);
assert_eq!(value, rng2.next_u32());Implementations§
Source§impl PseudoRng
impl PseudoRng
Sourcepub fn from_monotonic() -> Self
pub fn from_monotonic() -> Self
Construct a generator seeded from the system clock.
The seed mixes seconds and nanoseconds drawn from
SystemTime::now(). See the module-level docs for why we use
the system clock rather than a monotonic clock.
§Examples
use dynomite::hashkit::PseudoRng;
let mut rng = PseudoRng::from_monotonic();
let _ = rng.next_u32();Sourcepub fn from_seed(seed: u64) -> Self
pub fn from_seed(seed: u64) -> Self
Construct a generator from an explicit seed.
§Examples
use dynomite::hashkit::PseudoRng;
let mut a = PseudoRng::from_seed(7);
let mut b = PseudoRng::from_seed(7);
assert_eq!(a.next_u32(), b.next_u32());Sourcepub fn next_u32(&mut self) -> u32
pub fn next_u32(&mut self) -> u32
Advance the generator and return the next 32-bit value.
§Examples
use dynomite::hashkit::PseudoRng;
let mut rng = PseudoRng::from_seed(1);
let _: u32 = rng.next_u32();Sourcepub fn next_index(&mut self, modulus: u32) -> u32
pub fn next_index(&mut self, modulus: u32) -> u32
Pick a uniform value in [0, modulus). Returns 0 when modulus
is zero.
§Examples
use dynomite::hashkit::PseudoRng;
let mut rng = PseudoRng::from_seed(7);
for _ in 0..16 {
assert!(rng.next_index(13) < 13);
}
assert_eq!(rng.next_index(0), 0);Trait Implementations§
Auto Trait Implementations§
impl Freeze for PseudoRng
impl RefUnwindSafe for PseudoRng
impl Send for PseudoRng
impl Sync for PseudoRng
impl Unpin for PseudoRng
impl UnsafeUnpin for PseudoRng
impl UnwindSafe for PseudoRng
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more