/Curriculum/IntermediateRandom Generation

33. NumPy Random DistributionsIntermediate

Sample from Uniform, Normal, Binomial, Poisson, and Exponential distributions.

15 mins

Concept Overview

NumPy's random module supports statistical probability distributions essential for Monte Carlo simulations, physics modeling, and financial risk analysis.

Hardware Mental Model

Binomial models flipping a coin N times. Poisson models the number of customer arrivals per hour. Normal models human heights in a population.

Key Concepts (1)Click snippet to load in editor

`normal(loc, scale)`: Gaussian bell curve with center `loc` and width `scale`. `poisson(lam)`: Event arrivals with rate $\lambda$.

samples = np.random.normal(0, 1, 1000)

⚠ Common Mistakes & Pitfalls

Avoid these frequent beginner syntax and logic traps
❌ Incorrect:np.random.normal(10, 2) # Samples 1 scalar value
✓ Correct:np.random.normal(10, 2, size=100)

Include `size=N` to generate an entire array of sampled distribution points.

Pro Tip: Use `size=(rows, cols)` to sample multi-dimensional distribution matrices.

📌 Quick Revision

Core takeaway points from this topic
`np.random.uniform(low, high, size)`: Uniform continuous distribution.
`np.random.normal(loc, scale, size)`: Gaussian bell curve distribution.
`np.random.binomial(n, p, size)`: Discrete success counts.
`np.random.poisson(lam, size)`: Rate-based event occurrences.
Editor: 33. NumPy Random Distributions
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state

🎯 Try It Yourself: Practice Challenge

Hands-on Mode

Solve the objective below using NumPy vectorized syntax

Objective:Sample Normal Distribution

Set seed to 42, generate 3 normal samples with `loc=0`, `scale=1`, `size=3`, and print.

Expected Output Target:[ 0.497 -0.138 0.648]
Practice Workspace
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state