pub struct RandomState { /* private fields */ }Expand description
Random state for reproducible random number generation.
This struct holds the PRNG state and can be used with compiled functions to properly track random state across JIT compilation boundaries.
§Compilation Support
RandomState implements Updatable, making it compatible with
compile_with_state. This is the Rust equivalent of Python’s
@partial(mx.compile, inputs=mx.random.state, outputs=mx.random.state).
§Example
use mlx_rs::random::RandomState;
use mlx_rs::transforms::compile::compile_with_state;
use mlx_rs::random::categorical;
use mlx_rs::Array;
let mut state = RandomState::with_seed(42).unwrap();
let logits = Array::zeros::<f32>(&[1, 10]).unwrap();
let mut compiled = compile_with_state(
|state: &mut RandomState, x: &Array| {
let key = state.next_key()?;
categorical(x, None, None, Some(&key))
},
None
);
let result = compiled(&mut state, &logits).unwrap();Implementations§
Source§impl RandomState
impl RandomState
Sourcepub fn with_seed(seed: u64) -> Result<Self>
pub fn with_seed(seed: u64) -> Result<Self>
Create a new random state from a specific seed.
Use this for reproducible random number generation.
Sourcepub fn from_key(key: Array) -> Self
pub fn from_key(key: Array) -> Self
Create a random state from an existing key array.
The key must be a valid PRNG key (typically created via random::key()).
Sourcepub fn next_key(&mut self) -> Result<Array>
pub fn next_key(&mut self) -> Result<Array>
Get the next random key, advancing the state.
This splits the current state into two keys: one becomes the new state, and the other is returned for use in random operations.
Sourcepub fn as_array(&self) -> &Array
pub fn as_array(&self) -> &Array
Get a reference to the underlying state array.
This is useful for inspection or manual state management.
Sourcepub fn as_array_mut(&mut self) -> &mut Array
pub fn as_array_mut(&mut self) -> &mut Array
Get a mutable reference to the underlying state array.
§Note
Modifying the state array directly may break the PRNG invariants.
Prefer using seed() or next_key() instead.
Trait Implementations§
Source§impl Clone for RandomState
impl Clone for RandomState
Source§fn clone(&self) -> RandomState
fn clone(&self) -> RandomState
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for RandomState
impl Debug for RandomState
Source§impl Default for RandomState
impl Default for RandomState
Source§impl Updatable for RandomState
impl Updatable for RandomState
Source§fn state_projection(
&mut self,
) -> Result<StateProjection<'_>, StateProjectionError>
fn state_projection( &mut self, ) -> Result<StateProjection<'_>, StateProjectionError>
Auto Trait Implementations§
impl !Sync for RandomState
impl Freeze for RandomState
impl RefUnwindSafe for RandomState
impl Send for RandomState
impl Unpin for RandomState
impl UnsafeUnpin for RandomState
impl UnwindSafe for RandomState
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoOption<T> for T
impl<T> IntoOption<T> for T
Source§fn into_option(self) -> Option<T>
fn into_option(self) -> Option<T>
Option.