randomgen.generator.ExtendedGenerator.random

ExtendedGenerator.random(size=None, dtype='d', out=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 by (b-a) and add a:

(b - a) * random() + a
Parameters:
size=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.

dtype='d'

Desired dtype of the result. One of ‘d’ (‘float64’ or np.float64), ‘f’ (‘float32’ of np.float32), or ‘longdouble’ (np.longdouble). All dtypes are determined by their name. The default value is ‘d’.

out=None

Alternative output array in which to place the result. If size is not None, it must have the same shape as the provided size and must match the type of the output values.

Returns:

out – Array of random floats of shape size (unless size=None, in which case a single float is returned). If dtype is np.longdouble, then the returned type is a scalar np.longdouble. Otherwise it is a float.

Return type:

{float, longdouble or ndarray}

Notes

np.longdouble depends on the platform, and so do the values drawn with it. Each value uses one 64-bit draw where long double is the x87 80-bit format (x86-64 Linux and macOS) or the same as float64 (Windows and macOS on Apple silicon), and two 64-bit draws where it is a 128-bit format (for example Linux on ARM64 or POWER). The same seed therefore gives different long double values on these platforms, and on the 128-bit ones the later draws from the generator differ too. Use float64 or float32 when results must match across machines.

Examples

>>> randomgen.generator.random()
0.47108547995356098 # random
>>> type(randomgen.generator.random())
<class 'float'>
>>> randomgen.generator.random((3,))
array([ 0.30220482,  0.86820401,  0.1654503]) # random

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

>>> 5 * randomgen.generator.random((3, 2)) - 5
array([[-3.99149989, -0.52338984], # random
       [-2.99091858, -0.79479508],
       [-1.23204345, -1.75224494]])