bundles / numpy 2.5.0.dev0+git20251130.2de293a / numpy / random / RandomState / random_sample
cython_function_or_method
numpy.random:RandomState.random_sample
Signature
def random_sample(size = None)Summary
Return random floats in the half-open interval [0.0, 1.0).
Extended Summary
Results are from the "continuous uniform" distribution over the stated interval. To sample multiply the output of random_sample by (b-a) and add a:
(b - a) * random_sample() + aParameters
size: int or tuple of ints, optionalOutput shape. If the given shape is, e.g.,
(m, n, k), thenm * n * ksamples are drawn. Default is None, in which case a single value is returned.
Returns
out: float or ndarray of floatsArray of random floats of shape
size(unlesssize=None, in which case a single float is returned).
Examples
np.random.random_sample()
✗0.3407844228015793
type(np.random.random_sample())
✓<class 'float'>
np.random.random_sample((5,))
✗array([0.86763179, 0.20634753, 0.12471324, 0.26134683, 0.16323849])Three-by-two array of random numbers from [-5, 0):
5 * np.random.random_sample((3, 2)) - 5
✗array([[-3.32778385, -2.94402657],
[-1.89393131, -1.36020083],
[-2.09155673, -3.44408912]])
See also
- random.Generator.random
which should be used for new code.
Aliases
numpy.random.random_samplenumpy.random.RandomState.random_sample