randint

Contents

randint#

class brainstate.random.randint(low, high=None, size=None, key=None, dtype=None)#

Return random integers from low (inclusive) to high (exclusive).

Return random integers from the “discrete uniform” distribution of the specified dtype in the “half-open” interval [low, high). If high is None (the default), then results are from [0, low).

Parameters:
  • low (int or array-like of ints) – Lowest (signed) integers to be drawn from the distribution (unless high=None, in which case this parameter is one above the highest such integer).

  • high (int or array-like of ints, optional) – If provided, one above the largest (signed) integer to be drawn from the distribution (see above for behavior if high=None). If array-like, must contain integer values

  • size (int | Sequence[int] | integer | Sequence[integer] | None) – Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. Default is None, in which case a single value is returned.

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

  • dtype (str | type[Any] | dtype | SupportsDType) – Desired dtype of the result. Byteorder must be native. The default value is int.

Returns:

outsize-shaped array of random integers from the appropriate distribution, or a single such random int if size not provided.

Return type:

int or ndarray of ints

See also

random_integers

similar to randint, only for the closed interval [low, high], and 1 is the lowest value if high is omitted.

Generator.integers

which should be used for new code.

Examples

Generate 10 random integers from 0 to 1 (exclusive):

>>> import brainstate
>>> arr = brainstate.random.randint(2, size=10)
>>> print(arr.shape)  # (10,)
>>> print((arr >= 0).all() and (arr < 2).all())  # True

Generate a 2x4 array of integers from 0 to 4 (exclusive):

>>> arr = brainstate.random.randint(5, size=(2, 4))
>>> print(arr.shape)  # (2, 4)
>>> print((arr >= 0).all() and (arr < 5).all())  # True

Generate integers with different upper bounds using broadcasting:

>>> arr = brainstate.random.randint(1, [3, 5, 10])
>>> print(arr.shape)  # (3,)

Generate integers with different lower bounds:

>>> arr = brainstate.random.randint([1, 5, 7], 10)
>>> print(arr.shape)  # (3,)
>>> print((arr >= [1, 5, 7]).all())  # True