clone_rng

Contents

clone_rng#

class brainstate.random.clone_rng(seed_or_key=None, clone=True)#

Create a clone of the random state or a new random state.

This function provides a flexible way to create independent random states, either by cloning the current global state or by creating a new state with a specific seed or key. Cloned states are independent and don’t affect each other when used for random number generation.

Parameters:
  • seed_or_key (int | Array | ndarray) – Optional seed (integer) or JAX random key to initialize the new random state. If None, uses the current global state.

  • clone (bool) – Whether to clone the default random state. If False and seed_or_key is None, returns the global state directly (not recommended for most use cases as it shares state).

Return type:

RandomState

Returns:

A RandomState instance that can be used independently for random number generation.

Example

Clone the current global state:

>>> import brainstate
>>> brainstate.random.seed(42)
>>> rng1 = brainstate.random.clone_rng()
>>> rng2 = brainstate.random.clone_rng()
>>> # rng1 and rng2 are independent copies

Create a new state with specific seed:

>>> rng_fixed = brainstate.random.clone_rng(123)
>>> # Always produces the same sequences when reset to seed 123

Use for independent computations:

>>> rng = brainstate.random.clone_rng(456)
>>> values1 = rng.normal(size=5)
>>> values2 = rng.normal(size=5)
>>> # values1 and values2 are different but reproducible

Note

Cloned random states are completely independent. Changes to one state (like advancing through random number generation) don’t affect others.

See also