What is random uniform distribution?

This example shows how to generate random numbers using the uniform distribution inversion method. This is useful for distributions when it is possible to compute the inverse cumulative distribution function, but there is no support for sampling from the distribution directly.

Use rand to generate 1000 random numbers from the uniform distribution on the interval (0,1).

rng('default') % For reproducibility u = rand(1000,1);

The inversion method relies on the principle that continuous cumulative distribution functions (cdfs) range uniformly over the open interval (0,1). If u is a uniform random number on (0,1), then x=F-1(u) generates a random number x from any continuous distribution with the specified cdf F.

Step 2. Generate random numbers from the Weibull distribution.

Use the inverse cumulative distribution function to generate the random numbers from a Weibull distribution with parameters A = 1 and B = 1 that correspond to the probabilities in u. Plot the results.

x = wblinv(u,1,1); histogram(x,20);

The histogram shows that the random numbers generated using the Weibull inverse cdf function wblinv have a Weibull distribution.

Step 3. Generate random numbers from the standard normal distribution.

The same values in u can generate random numbers from any distribution, for example the standard normal, by following the same procedure using the inverse cdf of the desired distribution.

figure x_norm = norminv(u,1,1); histogram(x_norm,20)

The histogram shows that, by using the standard normal inverse cdf norminv, the random numbers generated from u now have a standard normal distribution.

See Also

wblinv | norminv | rand | hist

Apart from what is being mentioned above, .uniform() can also be used for generating multiple random numbers that too with the desired shape which is not possible with .random()

np.random.seed(99) np.random.random() #generates 0.6722785586307918

while the following code

np.random.seed(99) np.random.uniform(0.0, 1.0, size = (5,2)) #generates this array([[0.67227856, 0.4880784 ], [0.82549517, 0.03144639], [0.80804996, 0.56561742], [0.2976225 , 0.04669572], [0.9906274 , 0.00682573]])

This can't be done with random(...), and if you're generating the random(...) numbers for ML related things, most of the time, you'll end up using .uniform(...)

Examples of Uniform Distribution

Uniform distribution is the simplest statistical distribution. The concept of uniform distribution, as well as the random variables it describes, form the foundation of statistical analysis and probability theory.

For example, if you stand on a street corner and start to randomly hand a $100 bill to any lucky person who walks by, then every passerby would have an equal chance of being handed the money. The percentage of the probability is 1 divided by the total number of outcomes (number of passersby). However, if you favored short people or women, they would have a higher chance of being given the $100 bill than the other passersby. It would not be described as uniform probability.

A deck of cards also has a uniform distribution. It is because an individual has an equal chance of drawing a spade, a heart, a club, or a diamond. Another example of a uniform distribution is when a coin is tossed. The likelihood of getting a tail or head is the same. The graph of a uniform distribution is usually flat, whereby the sides and top are parallel to the x- and y-axes.

Types of Uniform Distribution

Uniform distribution can be grouped into two categories based on the types of possible outcomes.

1. Discrete uniform distribution

In statistics and probability theory, a discrete uniform distribution is a statistical distribution where the probability of outcomes is equally likely and with finite values. A good example of a discrete uniform distribution would be the possible outcomes of rolling a 6-sided die. The possible values would be 1, 2, 3, 4, 5, or 6. In this case, each of the six numbers has an equal chance of appearing. Therefore, each time the 6-sided die is thrown, each side has a chance of 1/6.

The number of values is finite. It is impossible to get a value of 1.3, 4.2, or 5.7 when rolling a fair die. However, if another die is added and they are both thrown, the distribution that results is no longer uniform because the probability of the sums is not equal. Another simple example is the probability distribution of a coin being flipped. The possible outcomes in such a scenario can only be two. Therefore, the finite value is 2.

There are several ways in which discrete uniform distribution can be valuable for businesses. For example, it can arise in inventory management in the study of the frequency of inventory sales. It can provide a probability distribution that can guide the business on how to properly allocate the inventory for the best use of square footage.

Discrete uniform distribution is also useful in Monte Carlo simulation. This is a modeling technique that uses programmed technology to identify the probabilities of different outcomes. Monte Carlo simulation is often used to forecast scenarios and help in the identification of risks.

2. Continuous uniform distribution

Not all uniform distributions are discrete; some are continuous. A continuous uniform distribution (also referred to as rectangular distribution) is a statistical distribution with an infinite number of equally likely measurable values. Unlike discrete random variables, a continuous random variable can take any real value within a specified range.

A continuous uniform distribution usually comes in a rectangular shape. A good example of a continuous uniform distribution is an idealized random number generator. With continuous uniform distribution, just like discrete uniform distribution, every variable has an equal chance of happening. However, there is an infinite number of points that can exist.

More Resources

To keep advancing your career, the additional CFI resources below will be useful:

Say I am working with a set of the numbers $\{1,2,3,4,5\}$ a uniform sampling of the numbers could result in the sequence $1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,...$. Another uniform sampling could result in $1,3,5,2,4,1,3,5,2,4,1,3,5,2,4,...$. Neither of these are random because my process for choosing did not allow things like $2,1$ in either case. My process was the obvious one given the sequences.

Now, consider the following random sampling algorithm. Flip a coin. If it is heads, return $1$ and if it is tails, return $3$. A valid sequence of outputs might be $1,1,3,1,3,3,1,1,3,3,3,1,1,1,3,1,3,1,1,1,3,3,3$. For the set $\{1,2,3,4,5\}$ this is clearly not uniform.

So this would suggest that there is no redundancy in saying "uniform and at random".

I would say that "random" refers to the process by which they are drawn, "uniform" refers to the property of the results of the process that each outcome is equally likely (distribution).

random.uniform(low=0.0, high=1.0, size=None)#

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.

Note

New code should use the uniform method of a default_rng() instance instead; please see the Quick Start.

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. The default value is 0.

highfloat or array_like of floats

Upper boundary of the output interval. All values generated will be less than or equal to high. The high limit may be included in the returned array of floats due to floating-point rounding in the equation low + (high-low) * random_sample(). 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. If size is None (default), a single value is returned if low and high are both scalars. Otherwise, np.broadcast(low, high).size samples are drawn.

Returns outndarray or scalar

Drawn samples from the parameterized uniform distribution.

See also

randint

Discrete uniform distribution, yielding integers.

random_integers

Discrete uniform distribution over the closed interval [low, high].

random_sample

Floats uniformly distributed over [0, 1).

random

Alias for random_sample.

rand

Convenience function that accepts dimensions as input, e.g., rand(2,2) would generate a 2-by-2 array of floats, uniformly distributed over [0, 1).

random.Generator.uniform

which should be used for new code.

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 of low will be returned. If high < 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. The high limit may be included in the returned array of floats due to floating-point rounding in the equation low + (high-low) * random_sample(). For example:

>>> x = np.float32(5*0.99999999) >>> x 5.0

Examples

Draw samples from the distribution:

>>> s = np.random.uniform(-1,0,1000)

All values are within the given interval:

>>> np.all(s >= -1) True >>> np.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, density=True) >>> plt.plot(bins, np.ones_like(bins), linewidth=2, color='r') >>> plt.show()

What is random uniform distribution?