larray.random.uniform
- larray.random.uniform(low=0.0, high=1.0, axes=None, meta=None) Array [source]
Draw 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 or array_like of floats, optional
Lower boundary of the output interval. All values generated will be greater than or equal to low. Defaults to 0.0.
- highfloat or array_like of floats, optional
Upper boundary of the output interval. All values generated will be less than high. Defaults to 1.0.
- axesint, tuple of int, str, Axis or tuple/list/AxisCollection of Axis, optional
Minimum axes the resulting array must have. Defaults to None. The resulting array axes will be the union of those mentioned in
axes
and those oflow
andhigh
. Iflow
andhigh
are scalars andaxes
is None, a single value is returned. Otherwise, if the resulting axes have a shape of, e.g.,(m, n, k)
, thenm * n * k
samples are drawn.- metalist of pairs or dict or Metadata, optional
Metadata (title, description, author, creation_date, …) associated with the array. Keys must be strings. Values must be of type string, int, float, date, time or datetime.
- Returns
- Array or scalar
Drawn samples from the parameterized uniform distribution.
See also
randint
Discrete uniform distribution, yielding integers.
Notes
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 oflow
will be returned. Ifhigh
<low
, the results are officially undefined and may eventually raise an error, i.e. do not rely on this function to behave when passed arguments satisfying that inequality condition.Examples
Generate a single sample from the distribution:
>>> la.random.uniform() 0.4616049008844396
Generate a 2 x 3 array with numbers drawn from the distribution:
>>> la.random.uniform(0, 5, axes=(2, 3)) {0}*\{1}* 0 1 2 0 3.4951791043804192 3.888533056628081 4.347461073315136 1 2.146211610940853 0.509146487437932 2.790852715735223
With named and labelled axes
>>> la.random.uniform(1, 2, axes='a=a0,a1;b=b0..b2') a\b b0 b1 b2 a0 1.4167729850467825 1.6953091052066793 1.2321770607672526 a1 1.4386221912579358 1.8480607144284926 1.1726213637670433
With varying low and high (each depending on a different axis)
>>> low = la.sequence('a=a0,a1') >>> low a a0 a1 0 1 >>> high = la.sequence('b=b0..b2', initial=1, inc=0.5) >>> high b b0 b1 b2 1.0 1.5 2.0 >>> la.random.uniform(low, high) a\b b0 b1 b2 a0 0.44608671494167573 0.948315996350121 1.74189664009661 a1 1.0 1.1099944474264194 1.1362792569316835
Draw 1000 samples from the distribution:
>>> s = la.random.uniform(-1, 0, 1000)
All values are within the given interval:
>>> la.all(s >= -1) True >>> la.all(s < 0) True
Display the histogram of the samples, along with the probability density function:
>>> import matplotlib.pyplot as plt >>> count, bins, ignored = plt.hist(s, 15, normed=True) >>> _ = plt.plot(bins, np.ones_like(bins), linewidth=2, color='r') >>> plt.show()