pub struct Random { /* private fields */ }Expand description
A pseudo-random number generator compatible with htm.core.
Uses ChaCha20 for high-quality randomness with deterministic behavior when seeded. The implementation aims to be reproducible across platforms.
§Example
use mokosh::utils::Random;
let mut rng = Random::new(42);
// Generate random integers
let n = rng.get_uint32();
// Generate random floats
let f = rng.get_real64();
// Sample from a range
let idx = rng.get_uint32_range(0, 100);
// Shuffle a vector
let mut items: Vec<u32> = (0..10).collect();
rng.shuffle(&mut items);Implementations§
Source§impl Random
impl Random
Sourcepub fn new(seed: i64) -> Self
pub fn new(seed: i64) -> Self
Creates a new random number generator with the given seed.
If seed is 0, uses a default seed (0 is a valid seed that produces deterministic output).
Sourcepub fn with_random_seed() -> Self
pub fn with_random_seed() -> Self
Creates a random number generator with a random seed.
Sourcepub fn get_uint32(&mut self) -> u32
pub fn get_uint32(&mut self) -> u32
Generates a random u32.
Sourcepub fn get_uint64(&mut self) -> u64
pub fn get_uint64(&mut self) -> u64
Generates a random u64.
Sourcepub fn get_uint32_range(&mut self, min: u32, max: u32) -> u32
pub fn get_uint32_range(&mut self, min: u32, max: u32) -> u32
Generates a random u32 in the range [min, max).
Sourcepub fn get_uint64_range(&mut self, min: u64, max: u64) -> u64
pub fn get_uint64_range(&mut self, min: u64, max: u64) -> u64
Generates a random u64 in the range [min, max).
Sourcepub fn get_real32(&mut self) -> f32
pub fn get_real32(&mut self) -> f32
Generates a random f32 in [0, 1).
Sourcepub fn get_real64(&mut self) -> f64
pub fn get_real64(&mut self) -> f64
Generates a random f64 in [0, 1).
Sourcepub fn get_real64_range(&mut self, min: f64, max: f64) -> f64
pub fn get_real64_range(&mut self, min: f64, max: f64) -> f64
Generates a random f64 in the range [min, max).
Sourcepub fn get_bool_with_prob(&mut self, probability: f64) -> bool
pub fn get_bool_with_prob(&mut self, probability: f64) -> bool
Generates a random boolean with the given probability of being true.
Sourcepub fn shuffle<T>(&mut self, slice: &mut [T])
pub fn shuffle<T>(&mut self, slice: &mut [T])
Shuffles a slice in place using Fisher-Yates algorithm.
Sourcepub fn sample<T: Clone>(&mut self, items: Vec<T>, k: usize) -> Vec<T>
pub fn sample<T: Clone>(&mut self, items: Vec<T>, k: usize) -> Vec<T>
Samples k unique items from a collection without replacement.
Returns a vector of k randomly selected items from the input.
If k >= items.len(), returns a shuffled copy of all items.
Sourcepub fn sample_indices(&mut self, n: usize, k: usize) -> Vec<usize>
pub fn sample_indices(&mut self, n: usize, k: usize) -> Vec<usize>
Samples k indices from 0..n without replacement.
Sourcepub fn normal(&mut self, mean: f64, std_dev: f64) -> f64
pub fn normal(&mut self, mean: f64, std_dev: f64) -> f64
Returns a normally distributed random number using Box-Muller transform.
Sourcepub fn exponential(&mut self, lambda: f64) -> f64
pub fn exponential(&mut self, lambda: f64) -> f64
Returns an exponentially distributed random number.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Random
impl RefUnwindSafe for Random
impl Send for Random
impl Sync for Random
impl Unpin for Random
impl UnwindSafe for Random
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)