Random

Struct Random 

Source
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

Source

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).

Source

pub fn with_random_seed() -> Self

Creates a random number generator with a random seed.

Source

pub fn seed(&self) -> u64

Returns the seed used for this generator.

Source

pub fn steps(&self) -> u64

Returns the number of random values generated.

Source

pub fn get_uint32(&mut self) -> u32

Generates a random u32.

Source

pub fn get_uint64(&mut self) -> u64

Generates a random u64.

Source

pub fn get_uint32_range(&mut self, min: u32, max: u32) -> u32

Generates a random u32 in the range [min, max).

Source

pub fn get_uint64_range(&mut self, min: u64, max: u64) -> u64

Generates a random u64 in the range [min, max).

Source

pub fn get_usize(&mut self, n: usize) -> usize

Generates a random usize in the range [0, n).

Source

pub fn get_real32(&mut self) -> f32

Generates a random f32 in [0, 1).

Source

pub fn get_real64(&mut self) -> f64

Generates a random f64 in [0, 1).

Source

pub fn get_real64_range(&mut self, min: f64, max: f64) -> f64

Generates a random f64 in the range [min, max).

Source

pub fn get_bool(&mut self) -> bool

Generates a random boolean with 50% probability.

Source

pub fn get_bool_with_prob(&mut self, probability: f64) -> bool

Generates a random boolean with the given probability of being true.

Source

pub fn shuffle<T>(&mut self, slice: &mut [T])

Shuffles a slice in place using Fisher-Yates algorithm.

Source

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.

Source

pub fn sample_indices(&mut self, n: usize, k: usize) -> Vec<usize>

Samples k indices from 0..n without replacement.

Source

pub fn normal(&mut self, mean: f64, std_dev: f64) -> f64

Returns a normally distributed random number using Box-Muller transform.

Source

pub fn exponential(&mut self, lambda: f64) -> f64

Returns an exponentially distributed random number.

Trait Implementations§

Source§

impl Clone for Random

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Random

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Random

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CloneToUninit for T
where T: Clone,

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V