numpy.random.uniform
functionA uniform distribution is a type of probability distribution where all outcomes in a given interval [a, b]
are equally likely. There are two main types of uniform distributions:
In a continuous uniform distribution, the probability density function (PDF) is given by:
[ f(x)=\begin{cases}\frac{1}{b - a},& \text{if }a\leq x\leq b\ 0,& \text{otherwise}\end{cases} ]
Here, a
is the lower bound and b
is the upper bound of the interval. The mean of a continuous uniform distribution is (\mu=\frac{a + b}{2}) and the variance is (\sigma^{2}=\frac{(b - a)^{2}}{12})
In a discrete uniform distribution, the probability mass function (PMF) assigns equal probability to a finite set of discrete values. For example, when rolling a fair six - sided die, each of the six possible outcomes ((1,2,\cdots,6)) has a probability of (\frac{1}{6})
numpy.random.uniform
functionThe numpy.random.uniform
function is used to generate random samples from a continuous uniform distribution. The syntax of the function is as follows:
numpy.random.uniform(low=0.0, high=1.0, size=None)
low
: The lower boundary of the output interval. The default value is 0.0.high
: The upper boundary of the output interval. The default value is 1.0.size
: The shape of the output array. If None
, a single value is returned.import numpy as np
# Generate a single random number between 0 and 1
single_random_num = np.random.uniform()
print("Single random number:", single_random_num)
# Generate an array of 5 random numbers between 2 and 5
random_array = np.random.uniform(low = 2, high = 5, size = 5)
print("Array of 5 random numbers:", random_array)
# Generate a 2D array (3x3) of random numbers between -1 and 1
random_2d_array = np.random.uniform(low=-1, high=1, size=(3, 3))
print("2D array of random numbers:\n", random_2d_array)
Uniform distributions can be used to simulate random events. For example, suppose we want to simulate the arrival time of customers at a store within a 1 - hour window (0 to 60 minutes). We can use the following code:
import numpy as np
import matplotlib.pyplot as plt
# Simulate arrival times of 100 customers
arrival_times = np.random.uniform(low = 0, high = 60, size = 100)
# Plot a histogram of arrival times
plt.hist(arrival_times, bins=20)
plt.xlabel('Arrival Time (minutes)')
plt.ylabel('Number of Customers')
plt.title('Simulated Customer Arrival Times')
plt.show()
Uniform distributions are often used for sampling in statistical analysis. For instance, when we want to estimate the area under a curve using the Monte Carlo method, we can sample points uniformly from a given region.
import numpy as np
# Define a function y = x^2
def func(x):
return x**2
# Generate 1000 random points in the interval [0, 1]
x_samples = np.random.uniform(low = 0, high = 1, size = 1000)
y_samples = func(x_samples)
# Estimate the area under the curve
area_estimate = np.mean(y_samples)
print("Estimated area under the curve:", area_estimate)
When working with random numbers, it is often necessary to reproduce the same set of random numbers for debugging or comparison purposes. You can use numpy.random.seed
to set a seed value.
import numpy as np
# Set the seed
np.random.seed(42)
random_numbers_1 = np.random.uniform(low = 0, high = 1, size = 3)
# Set the same seed again
np.random.seed(42)
random_numbers_2 = np.random.uniform(low = 0, high = 1, size = 3)
print("First set of random numbers:", random_numbers_1)
print("Second set of random numbers:", random_numbers_2)
When specifying the size
parameter, make sure to understand the shape requirements of your application. Incorrect shapes can lead to unexpected results. For example, if you want a 1D array of 10 elements, use size = 10
, and if you want a 2D array of shape (2, 5)
, use size=(2, 5)
NumPy’s uniform distribution functions provide a simple and efficient way to generate random samples from a continuous uniform distribution. By understanding the fundamental concepts, usage methods, common practices, and best practices, you can leverage these functions to simulate random events, perform statistical analysis, and solve a wide range of numerical problems. Whether you are a beginner in data science or an experienced practitioner, mastering the NumPy uniform distribution is an essential skill.