default_rng

Contents

default_rng#

class brainstate.random.default_rng(seed_or_key=None)#

Get the default random state or create a new one with specified seed.

This function provides access to the global random state used throughout BrainState, or creates a new independent random state if a seed is provided. It’s the primary interface for obtaining random state objects in BrainState.

Parameters:

seed_or_key (int | Array | ndarray) – Optional seed (integer) or JAX random key. If None, returns the global default random state. If provided, creates a new independent RandomState with the specified seed.

Return type:

RandomState

Returns:

The default RandomState if seed_or_key is None, otherwise a new RandomState initialized with the provided seed or key.

Example

Get the global random state:

>>> import brainstate
>>> rng = brainstate.random.default_rng()
>>> # rng is the global random state used by brainstate.random functions

Create a new independent random state:

>>> rng_local = brainstate.random.default_rng(42)
>>> values = rng_local.normal(size=10)

Use for reproducible local computations:

>>> def reproducible_computation():
...     local_rng = brainstate.random.default_rng(12345)
...     return local_rng.uniform(size=5)
>>> result1 = reproducible_computation()
>>> result2 = reproducible_computation()
>>> assert np.allclose(result1, result2)  # Always the same

Note

When seed_or_key is None, this returns the actual global state object. Modifications to this state (through random number generation) will affect all subsequent calls to global random functions.

See also

  • clone_rng(): Create independent clones of random states

  • seed(): Set the global random seed

  • RandomState: The underlying random state implementation