Skip to main content

Introduction to Python Uniform Distribution

Python Uniform Distribution

The uniform distribution, also known as the rectangular distribution, is a continuous probability distribution where all values within a given range are equally likely to occur.

In other words, the probability density function (PDF) of a uniform distribution is constant over a defined interval and zero outside that interval.

The probability density function of a uniform distribution is given by:

f(x) = 1 / (b - a)

Where 'a' and 'b' are the lower and upper bounds of the interval, respectively.

In Python, you can generate random numbers from a uniform distribution using the numpy.random.uniform() function from the NumPy library.

As an example:

import numpy as np

# Generate random numbers from a uniform distribution
a = 0 # Lower bound
b = 10 # Upper bound
size = 1000 # Number of random numbers to generate

random_numbers = np.random.uniform(a, b, size)

In this example:

  • np.random.uniform(a, b, size) generates an array of 1000 random numbers from a uniform distribution between 0 (inclusive) and 10 (exclusive).
  • You can adjust the values of 'a' and 'b' to define the desired interval for the distribution.
  • The resulting array random_numbers will contain the generated random numbers.