mygrad.random.random_sample#

mygrad.random.random_sample(shape: Shape | None = None, *, constant: bool | None = None) Tensor[source]#

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

Results are from the “continuous uniform” distribution over the stated interval.

To create a random sample of a given shape on the interval [a, b), call (b-a) * random_sample(shape) + a

Parameters:
shape: int or tuple of ints, optional

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.

constantOptional[bool]

If True, this tensor is treated as a constant, and thus does not facilitate back propagation (i.e. constant.grad will always return None).

Defaults to False for float-type data. Defaults to True for integer-type data.

Integer-type tensors must be constant.

Returns
——-
int or mygrad.Tensor of ints

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

Examples

>>> from mygrad.random import random_sample
>>> random_sample((3, 2))
Tensor([[0.76490814, 0.69378441],
        [0.65228375, 0.68395309],
        [0.08228869, 0.03191064]])
>>> random_sample()
Tensor(0.47644928)