numpy.random.Generator.random#
method
- random.Generator.random(size=None,dtype=np.float64,out=None)#
Return random floats in the half-open interval [0.0, 1.0).
Results are from the “continuous uniform” distribution over thestated interval. To sample\(Unif[a, b), b > a\) use
uniformor multiply the output ofrandomby(b-a)and adda:(b-a)*random()+a
- Parameters:
- sizeint or tuple of ints, optional
Output shape. If the given shape is, e.g.,
(m,n,k), thenm*n*ksamples are drawn. Default is None, in which case asingle value is returned.- dtypedtype, optional
Desired dtype of the result, only
float64andfloat32are supported.Byteorder must be native. The default value is np.float64.- outndarray, optional
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 ofthe output values.
- Returns:
- outfloat or ndarray of floats
Array of random floats of shape
size(unlesssize=None, in whichcase a single float is returned).
See also
uniformDraw samples from the parameterized uniform distribution.
Examples
>>>rng=np.random.default_rng()>>>rng.random()0.47108547995356098 # random>>>type(rng.random())<class 'float'>>>>rng.random((5,))array([ 0.30220482, 0.86820401, 0.1654503 , 0.11659149, 0.54323428]) # random
Three-by-two array of random numbers from [-5, 0):
>>>5*rng.random((3,2))-5array([[-3.99149989, -0.52338984], # random [-2.99091858, -0.79479508], [-1.23204345, -1.75224494]])