self_assign_multi_keys

self_assign_multi_keys#

class brainstate.random.self_assign_multi_keys(n, backup=True)#

Assign multiple keys to the global random state for parallel access.

This function prepares the global random state for parallel computation by pre-generating n independent keys. It’s particularly useful when you need to ensure that parallel computations have access to independent random sequences without the overhead of key splitting during computation.

Parameters:
  • n (int) – The number of independent keys to pre-generate and assign. Must be a positive integer.

  • backup (bool) – Whether to backup the current random state before assignment. If True, the original state can be restored using restore_key().

Raises:

ValueError – If n is not a positive integer.

Return type:

None

Example

Prepare for parallel computation:

>>> import brainstate
>>> brainstate.random.seed(42)
>>> # Prepare 4 independent keys for parallel access
>>> brainstate.random.self_assign_multi_keys(4)

Use in parallel context:

>>> # The random state now has 4 independent keys ready for use
>>> # Each parallel thread can access a different key

Note

This is an advanced function primarily used internally for optimizing parallel random number generation. In most cases, split_keys() provides a more straightforward interface for parallel computation.

See also