Exploring Random Number Generation with Different Distributions in Python
Tags: python, computerscience, math, statistics
Published: 2023-10-28 07:44:18

Generating random numbers with different distributions is a fascinating topic that has applications in many areas, from computer simulations to machine learning.
 
**What are distributions?**
In statistics, a distribution describes the likelihood of an event occurring. In the context of random numbers, a distribution describes how likely each number is to be selected.

For this article i generated random numbers with 3 different distributions: Uniform, Normal and Exponential.

**Uniform Distribution**
The uniform distribution is the simplest of all distributions. In a uniform distribution, all numbers within a specific range have the same likelihood of being selected. It is useful when you want all possible outcomes to be equally likely.

`np.random.uniform(low=1, high=1000, size=10000)`

In real life, the uniform distribution is often used in computer simulations (e.g. [Monte Carlo method](https://en.wikipedia.org/wiki/Monte_Carlo_method)) and games where you want all outcomes to be equally likely.

**Normal Distribution**
Also known as the Gaussian distribution, is probably the most well-known of all distributions. In a normal distribution, numbers near the middle of the range have a higher likelihood of being selected, while numbers at the ends of the range have a lower likelihood.

`random.normal(loc=500, scale=100, size=10000)`

In real life, the normal distribution is used in a wide variety of fields including statistics, physics and engineering. It’s useful when you’re working with data that clusters around an average.

**Exponential Distribution**
The exponential distribution is a bit different. In an exponential distribution, smaller numbers have a higher likelihood of being selected and this likelihood decreases exponentially as the numbers increase.

`random.exponential(scale=100, size=10000)`

In real life, the exponential distribution is often used in queueing theory and in modeling the lifetime of certain products and materials.

This graphs represent the results and allow see which numbers are more likehood to be selected in the different distributions:


![Results](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zdpd2u0dzwvrzb9t4799.png)

The Uniform Distribution shows an almost constant bar across the full range (0-1000), indicating that each number within this range has roughly equal probability of being selected. 

The Normal Distribution shows a “bell curve”, indicating that numbers near the middle of the range (around 500) have a higher probability of being selected while numbers at the ends of the range have a lower probability.. 

The Exponential Distribution shows a rapidly decreasing curve, indicating that smaller numbers have a higher probability of being selected and this probability decreases exponentially as the numbers increase.

**Full Code**

```
import numpy as np
import matplotlib.pyplot as plt

# Define the number of samples and the range of the random numbers
num_samples = 10000
low, high = 1, 1000

# Generate random numbers with different distributions
uniform_dist = np.random.uniform(low=low, high=high, size=num_samples)
normal_dist = np.random.normal(loc=high/2, scale=(high-low)/10, size=num_samples)
exponential_dist = np.random.exponential(scale=(high-low)/10, size=num_samples) 

plt_uniform_dist = np.clip(uniform_dist, low, high)
plt_normal_dist = np.clip(normal_dist, low, high)
plt_exponential_dist = np.clip(exponential_dist, low, high)

plt.figure(figsize=(15, 5))

plt.subplot(1, 3, 1)
plt.hist(plt_uniform_dist, bins=50, color='c')
plt.title('Uniform Distribution')

plt.subplot(1, 3, 2)
plt.hist(plt_normal_dist, bins=50, color='m')
plt.title('Normal Distribution')

plt.subplot(1, 3, 3)
plt.hist(plt_exponential_dist, bins=50, color='y')
plt.title('Exponential Distribution')

plt.tight_layout()
plt.show()

```