Table of Contents
ToggleA uniform distribution is a type of probability distribution where all outcomes are equally likely. This means that the probability of any given outcome is constant across the range of possible outcomes.
Uniform distributions can be continuous or discrete. In a continuous uniform distribution, the outcomes can take any value within a specified range. In contrast, a discrete uniform distribution has a finite set of possible outcomes.
NumPy provides the numpy.random.uniform() function to generate samples from a continuous uniform distribution. This function allows you to specify the range and size of the generated samples.
In this example, we generate 10 random samples from a uniform distribution between 0 and 1 −
# Open Compiler
import numpy as np
# Generate 10 random samples from a uniform distribution between 0 and 1
samples = np.random.uniform(0, 1, 10)
print("Random samples from uniform distribution:", samples)
Output:
Following is the output obtained −
Random samples from uniform distribution: [0.70748409 0.45654756 0.73426382 0.15580835 0.70294526 0.12503631 0.40303738 0.9862709 0.4923119 0.44059809]
Visualizing uniform distributions helps to understand their properties better. We can use libraries such as Matplotlib to create histograms that display the distribution of generated samples.
In the following example, we are generating 1000 random samples from a uniform distribution between 0 and 1 and then create a histogram to visualize this distribution −
import numpy as np
import matplotlib.pyplot as plt
# Generate 1000 random samples from a uniform distribution between 0 and 1
samples = np.random.uniform(0, 1, 1000)
# Create a histogram to visualize the distribution
plt.hist(samples, bins=30, edgecolor='black')
plt.title('Uniform Distribution')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.show()
Output:
The histogram shows that the samples are uniformly distributed between 0 and 1, with an approximately equal frequency for each bin −
Uniform Distribution
Uniform distributions are used in various fields, including computer simulations, Monte Carlo methods, and random sampling. Here are a few practical applications −
NumPy also allows generating samples from a discrete uniform distribution using the numpy.random.randint() function. This function generates random integers within a specified range.
In this example, we generate 10 random integers between 1 and 10, inclusive −
# Open Compiler
import numpy as np
# Generate 10 random integers between 1 and 10
samples = np.random.randint(1, 11, 10)
print("Random samples from discrete uniform distribution:", samples)
Output:
Following is the output obtained −
Random samples from discrete uniform distribution: [ 7 3 9 10 9 4 8 4 1 7]
Uniform distributions have several key properties, they are as follows −
You can calculate the mean and variance of a uniform distribution using simple formulas. Let us see how to calculate the mean and variance for a uniform distribution between a and b.
In this example, the mean and variance of the uniform distribution between 0 and 1 are calculated −
# Open Compiler
import numpy as np
# Define the range of the uniform distribution
a = 0
b = 1
# Calculate the mean and variance
mean = (a + b) / 2
variance = ((b - a) ** 2) / 12
print("Mean:", mean)
print("Variance:", variance)
Output:
Following is the output obtained −
Mean: 0.5
Variance: 0.08333333333333333
NumPy can generate uniform distributions for multidimensional arrays as well. Here is an example:
In this example, a 3×3 array of random samples from a uniform distribution between 0 and 1 is generated −
# Open Compiler
import numpy as np
# Generate a 3x3 array of random samples from a uniform distribution between 0 and 1
samples = np.random.uniform(0, 1, (3, 3))
print("3x3 array of random samples from uniform distribution:", samples)
Output:
The output obtained is as shown below −
3×3 array of random samples from uniform distribution:
[[0.18528116 0.65725829 0.06597822]
[0.73183704 0.05931206 0.65555952]
[0.92479579 0.89807463 0.02624335]]
To ensure reproducibility, you can set a specific seed before generating uniform distributions. This ensures that the same sequence of random numbers is generated each time you run the code.
By setting the seed, you ensure that the random generation produces the same result every time the code is executed as shown in the following example −
# Open Compiler
import numpy as np
# Set the seed for reproducibility
np.random.seed(42)
# Generate 10 random samples from a uniform distribution between 0 and 1
samples = np.random.uniform(0, 1, 10)
print("Random samples with seed 42:", samples)
Output:
Following is the output obtained −
Random samples with seed 42: [0.37454012 0.95071431 0.73199394 0.59865848 0.15601864 0.15599452 0.05808361 0.86617615 0.60111501 0.70807258]
Key Takeaway: Master uniform distributions with NumPy at Vista Academy!
