nlcpy.random.Generator.uniform

Generator.uniform(self, low=0.0, high=1.0, size=None)

Draws samples from a uniform distribution.

Samples are uniformly distributed over the half-open interval [low, high) (includes low, but excludes high). In other words, any value within the given interval is equally likely to be drawn by uniform.

Parameters
lowfloat, optional

Lower boundary of the output interval. All values generated will be greater than or equal to low. The default value is 0.

highfloat

Upper boundary of the output interval. All values generated will be less than high. The default value is 1.0.

sizeint or tuple of ints, optional

Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn.

Returns
outndarray

Drawn samples from the parameterized uniform distribution.

See also

Generator.integers

Returns random integers.

Generator.random

Returns random floats.

Note

The probability density function of the uniform distribution is

p(x) = \frac{1}{b - a}

anywhere within the interval [a, b), and zero elsewhere.

When high == low, values of low will be returned.

Restriction

  • If low is neither a scalar nor None : NotImplementedError occurs.

  • If high is neither a scalar nor None : NotImplementedError occurs.

Examples

Draw samples from the distribution:

>>> import nlcpy as vp
>>> s = vp.random.default_rng().uniform(-1,0,1000)

All values are within the given interval:

>>> vp.all(s >= -1)
array(True)
>>> vp.all(s < 0)
array(True)
>>> import matplotlib.pyplot as plt
>>> count, bins, ignored = plt.hist(s.get(), 15, density=True)
>>> plt.plot(bins, vp.ones_like(bins),
... linewidth=2, color='r') 
>>> plt.show()
../../_images/nlcpy-random-Generator-uniform-1.png