random_sample

Contents

random_sample#

class brainstate.random.random_sample(size=None, key=None, dtype=None)#

Return random floats in the half-open interval [0.0, 1.0).

Results are from the “continuous uniform” distribution over the stated interval. To sample \(Unif[a, b), b > a\) multiply the output of random_sample by (b-a) and add a:

(b - a) * random_sample() + a
Parameters:
  • 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:

out – Array of random floats of shape size (unless size=None, in which case a single float is returned).

Return type:

float or ndarray of floats

See also

Generator.random

which should be used for new code.

Examples

Generate a single random float:

>>> import brainstate
>>> val = brainstate.random.random_sample()
>>> print(type(val))  # <class 'float'>
>>> print(0.0 <= val < 1.0)  # True

Generate an array of 5 random floats:

>>> arr = brainstate.random.random_sample((5,))
>>> print(arr.shape)  # (5,)
>>> print((arr >= 0.0).all() and (arr < 1.0).all())  # True

Three-by-two array of random numbers from [-5, 0):

>>> arr = 5 * brainstate.random.random_sample((3, 2)) - 5
>>> print(arr.shape)  # (3, 2)
print((arr >= -5.0).all() and (arr < 0.0).all())  # True