shuffle

Contents

shuffle#

class brainstate.random.shuffle(x, axis=0, key=None)#

Modify a sequence in-place by shuffling its contents.

This function only shuffles the array along the first axis of a multi-dimensional array. The order of sub-arrays is changed but their contents remains the same.

Parameters:
  • x (ndarray or MutableSequence) – The array, list or mutable sequence to be shuffled.

  • key (int | Array | ndarray | None) – The key for the random number generator. If not given, the default random number generator is used.

Return type:

None

Examples

Shuffle a 1D array in-place:

>>> import brainstate
>>> import numpy as np
>>> arr = np.arange(10)
>>> original_elements = set(arr)
>>> brainstate.random.shuffle(arr)
>>> print(set(arr) == original_elements)  # True (same elements)

Multi-dimensional arrays are only shuffled along the first axis:

>>> arr = np.arange(9).reshape((3, 3))
>>> original_shape = arr.shape
>>> brainstate.random.shuffle(arr)
>>> print(arr.shape == original_shape)  # True (shape preserved)
>>> print(sorted(arr.flatten()) == list(range(9)))  # True (same elements)