random_integers

Contents

random_integers#

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

Random integers of type np.int_ between low and high, inclusive.

Return random integers of type np.int_ from the “discrete uniform” distribution in the closed interval [low, high]. If high is None (the default), then results are from [1, low]. The np.int_ type translates to the C long integer type and its precision is platform dependent.

Parameters:
  • low (int) – Lowest (signed) integer to be drawn from the distribution (unless high=None, in which case this parameter is the highest such integer).

  • high (int, optional) – If provided, the largest (signed) integer to be drawn from the distribution (see above for behavior if high=None).

  • 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.

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

randint

Similar to random_integers, only for the half-open interval [low, high), and 0 is the lowest value if high is omitted.

Notes

To sample from N evenly spaced floating-point numbers between a and b, use:

a + (b - a) * (brainstate.random.random_integers(N) - 1) / (N - 1.)

Examples

Generate a single random integer from 1 to 5 (inclusive):

>>> import brainstate
>>> val = brainstate.random.random_integers(5)
>>> print(type(val))  # <class 'numpy.int64'>
>>> print(1 <= val <= 5)  # True

Generate a 3x2 array of random integers from 1 to 5 (inclusive):

>>> arr = brainstate.random.random_integers(5, size=(3, 2))
>>> print(arr.shape)  # (3, 2)
>>> print((arr >= 1).all() and (arr <= 5).all())  # True

Choose five random numbers from the set of five evenly-spaced numbers between 0 and 2.5, inclusive (i.e., from the set \({0, 5/8, 10/8, 15/8, 20/8}\)):

>>> vals = 2.5 * (brainstate.random.random_integers(5, size=(5,)) - 1) / 4.
>>> print(vals.shape)  # (5,)

Roll two six sided dice 1000 times and sum the results:

>>> d1 = brainstate.random.random_integers(1, 6, 1000)
>>> d2 = brainstate.random.random_integers(1, 6, 1000)
>>> dsums = d1 + d2
>>> print(dsums.shape)  # (1000,)
>>> print((dsums >= 2).all() and (dsums <= 12).all())  # True