Skip to main content

RandomState

Struct RandomState 

Source
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

Source

pub fn new() -> Result<Self>

Create a new random state with a time-based seed.

Source

pub fn with_seed(seed: u64) -> Result<Self>

Create a new random state from a specific seed.

Use this for reproducible random number generation.

Source

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

Source

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.

Source

pub fn seed(&mut self, seed: u64) -> Result<()>

Reseed the random state.

Source

pub fn as_array(&self) -> &Array

Get a reference to the underlying state array.

This is useful for inspection or manual state management.

Source

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

Source§

fn clone(&self) -> RandomState

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for RandomState

Source§

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

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

impl Default for RandomState

Source§

fn default() -> Self

Creates a new RandomState with a time-based seed.

§Panics

Panics if the underlying PRNG key creation fails, which should not occur under normal conditions.

Source§

impl Updatable for RandomState

Source§

fn state_projection( &mut self, ) -> Result<StateProjection<'_>, StateProjectionError>

Declare all required and optional state slots.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

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

Source§

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
Source§

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

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> IntoOption<T> for T

Source§

fn into_option(self) -> Option<T>

Convert into an Option.
Source§

impl<T> IntoStrideBy for T

Source§

fn stride_by(self, stride: i32) -> StrideBy<T>

Create a stride indexing operation.
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

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

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

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

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

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.