Other Common Random Distributions in Python

📊 Other Common Random Distributions in Python

After Normal (Gaussian) distribution, NumPy can generate other statistical distributions like Uniform, Binomial, Poisson, and Exponential. Let’s go through each with examples and visualization.


1️⃣ Uniform Distribution

Generates numbers evenly distributed between low and high.

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# Generate uniform data
data = np.random.uniform(low=0, high=10, size=1000)

# Histogram
sns.histplot(data, bins=30, kde=True, color=‘skyblue’)
plt.title(“Uniform Distribution”)
plt.xlabel(“Value”)
plt.ylabel(“Frequency”)
plt.show()

  • Values are equally likely between 0 and 10

  • kde=True shows the smooth density curve


2️⃣ Binomial Distribution

Simulates success/failure experiments, e.g., coin flips.

# Parameters
n = 10 # number of trials
p = 0.5 # probability of success
size = 1000
# Generate binomial data
data = np.random.binomial(n, p, size)

# Visualization
sns.histplot(data, bins=10, kde=False, color=‘orange’)
plt.title(“Binomial Distribution (n=10, p=0.5)”)
plt.xlabel(“Number of Successes”)
plt.ylabel(“Frequency”)
plt.show()

  • Peaks around n*p (expected value)

  • Discrete distribution


3️⃣ Poisson Distribution

Models count of events in a fixed interval.

lam = 5 # expected number of events
size = 1000
data = np.random.poisson(lam, size)

sns.histplot(data, bins=15, kde=False, color=‘green’)
plt.title(“Poisson Distribution (λ=5)”)
plt.xlabel(“Number of Events”)
plt.ylabel(“Frequency”)
plt.show()

  • Values are integers (0,1,2…)

  • Mean ≈ λ


4️⃣ Exponential Distribution

Models time between events, continuous.

scale = 2.0 # 1/rate
size = 1000
data = np.random.exponential(scale, size)

sns.histplot(data, bins=30, kde=True, color=‘purple’)
plt.title(“Exponential Distribution (scale=2.0)”)
plt.xlabel(“Value”)
plt.ylabel(“Frequency”)
plt.show()

  • Values ≥ 0

  • Skewed distribution, decays exponentially


5️⃣ Multinomial Distribution

Used for categorical outcomes with probabilities.

n = 10
pvals = [0.2, 0.3, 0.5] # probabilities of each category
size = 5
data = np.random.multinomial(n, pvals, size)
print(data)

Output (example):

[[2 3 5]
[1 3 6]
[2 2 6]
[1 4 5]
[0 3 7]]
  • Each row = number of outcomes per trial

  • Each column = category


🧠 Summary Table of Distributions

Distribution Function Key Parameters Type
Normal np.random.normal() loc, scale, size Continuous
Uniform np.random.uniform() low, high, size Continuous
Binomial np.random.binomial() n, p, size Discrete
Poisson np.random.poisson() lam, size Discrete
Exponential np.random.exponential() scale, size Continuous
Multinomial np.random.multinomial() n, pvals, size Discrete

🎯 Practice Exercises

  1. Generate 5000 numbers from uniform distribution (0–100) and plot histogram.

  2. Simulate 100 coin flips with probability 0.3 and plot frequency of heads.

  3. Generate Poisson data with λ=7 and visualize it.

  4. Generate exponential data with scale=3 and plot histogram + KDE.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *