Random Data Distribution

📊 NumPy Random Data Distribution (Complete Guide)

NumPy provides functions to generate random numbers following different statistical distributions. This is very useful in simulations, data analysis, and machine learning.

All these functions are in the numpy.random module.


✅ 1. Uniform Distribution

Generates numbers evenly distributed over a range [low, high).

from numpy import random

arr = random.uniform(low=0.0, high=10.0, size=5)
print(arr)

Output (example):

[1.23 7.45 3.67 0.89 9.12]
  • low → Minimum value

  • high → Maximum value

  • size → Number of values or shape


✅ 2. Normal (Gaussian) Distribution

Generates numbers based on bell curve with mean loc and standard deviation scale.

arr = random.normal(loc=0, scale=1, size=5)
print(arr)

Output (example):

[0.45, -0.88, 1.23, 0.67, -1.12]
  • loc → Mean

  • scale → Standard deviation

  • size → Output shape


✅ 3. Binomial Distribution

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

arr = random.binomial(n=10, p=0.5, size=5)
print(arr)

Output (example):

[5 6 4 7 5]
  • n → Number of trials

  • p → Probability of success


✅ 4. Poisson Distribution

Used for count of events in a fixed interval.

arr = random.poisson(lam=3, size=5)
print(arr)

Output (example):

[2 4 3 1 5]
  • lam → Expected number of events


✅ 5. Exponential Distribution

Simulates time between events.

arr = random.exponential(scale=2.0, size=5)
print(arr)
  • scale → 1 / rate parameter

  • size → Number of samples


✅ 6. Multinomial Distribution

Used for multiple categorical outcomes.

arr = random.multinomial(n=10, pvals=[0.2, 0.3, 0.5], size=5)
print(arr)

Output (example):

[[1 3 6]
[2 4 4]
[3 2 5]
[0 2 8]
[2 3 5]]
  • n → Number of trials

  • pvals → Probabilities for each category

  • size → Number of experiments


✅ 7. Shuffle & Permutation

  • shuffle() → Shuffle in place

  • permutation() → Return shuffled copy

arr = np.arange(10)
random.shuffle(arr)
print(arr)

perm = random.permutation(np.arange(10))
print(perm)


✅ 8. Seeding Random Generators

random.seed(42) # Ensures reproducibility

🧠 Summary Table of Common Distributions

Function Distribution Parameters
uniform() Uniform low, high, size
normal() Gaussian loc, scale, size
binomial() Binomial n, p, size
poisson() Poisson lam, size
exponential() Exponential scale, size
multinomial() Multinomial n, pvals, size
shuffle() Shuffle array
permutation() Shuffled copy array

🎯 Practice Tasks

  1. Generate 1000 random numbers from normal distribution (mean=50, std=5)

  2. Simulate coin flips 10 times for 50 trials

  3. Generate 5 random arrays following Poisson distribution (lam=3)

  4. Shuffle a 1D array of 20 elements

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 *