import numpy as np
np.random.seed(42)
normal_samples = np.random.normal(loc=0.0, scale=1.0, size=(2, 3))
print(normal_samples)
numpy.random module provides comprehensive routines to draw random samples from various probability distributions, essential for Monte Carlo simulations, statistical modeling, and machine learning initialization.Understanding statistical distributions allows you to model real-world stochastic processes accurately. This guide covers normal (Gaussian) distributions via np.random.normal(), continuous uniform distributions via np.random.uniform(), discrete binomial trials via np.random.binomial(), and Poisson event modeling via np.random.poisson(). For related random number generators, review randn() and random_sample().
| Distribution Function | Parameters | Mathematical Purpose |
|---|---|---|
np.random.normal(loc, scale, size) | loc (mean), scale (std dev), size | Draws random samples from a normal (Gaussian) distribution |
np.random.uniform(low, high, size) | low, high, size | Draws samples from a uniform continuous distribution over [low, high) |
np.random.binomial(n, p, size) | n (trials), p (probability), size | Draws samples from a binomial distribution |
np.random.poisson(lam, size) | lam (rate / expected events), size | Draws samples from a Poisson distribution |
The normal distribution is parameterized by mean (loc) and standard deviation (scale):
import numpy as np
np.random.seed(42)
mu, sigma = 50.0, 5.0
samples = np.random.normal(loc=mu, scale=sigma, size=1000)
print("Sample mean (approx 50):", np.mean(samples))
print("Sample std (approx 5):", np.std(samples))
assert round(np.mean(samples), 0) == 50.0
Expected output
Sample mean (approx 50): 50.08271501980839
Sample std (approx 5): 4.981832014164132
The table below highlights how legacy distribution snippets compare against the modernized, structured implementation:
| Aspect | Legacy Implementation | Modernized Implementation |
|---|---|---|
| Structure & Markup | Basic echo output with unformatted pre tags and missing metadata. | Structured HTML5 layout, Schema TechArticle markup, and responsive tables. |
| Code Presentation | Requires scrolling past introductory text to find code examples. | Core code snippet placed right at the top for instant visitor access. |
| Verification | Unchecked print statements with manual result inspection comments. | Automated unit test assertions (assert) verifying statistical parameters. |
| Ecosystem Integration | Isolated code blocks with sparse navigation links. | Comprehensive cross-linking, quick-jump navigation, and runnable Colab notebooks. |
Draws samples uniformly over the interval [low, high):
np.random.seed(42)
u_samples = np.random.uniform(low=10.0, high=20.0, size=5)
print("Uniform samples in [10, 20):", u_samples)
assert np.all(u_samples >= 10.0) and np.all(u_samples < 20.0)
Expected output
Uniform samples in [10, 20): [17.73956054 14.38878442 18.58597918 16.97368032 15.24674516]
Models the number of successes in n independent yes/no experiments with success probability p:
np.random.seed(42)
# 10 coin flips per trial, 0.5 probability, repeated 5 times
b_samples = np.random.binomial(n=10, p=0.5, size=5)
print("Binomial successes:", b_samples)
assert b_samples.shape == (5,)
Expected output
Binomial successes: [7 4 6 6 5]
Models the probability of a given number of events occurring in a fixed interval of time or space given rate parameter lam:
np.random.seed(42)
p_samples = np.random.poisson(lam=3.0, size=5)
print("Poisson event counts:", p_samples)
assert p_samples.shape == (5,)
Expected output
Poisson event counts: [3 1 3 2 2]
scale in np.random.normal() raises a ValueError.p in np.random.binomial() must satisfy 0 <= p <= 1.randn() draws samples specifically from the standard normal distribution (mean=0, std=1) with positional shape arguments. normal(loc, scale, size) allows explicit configuration of custom means and standard deviations using keyword arguments.
Test statistical distributions, parameter tuning, random sampling, and run verification test suites in our interactive Google Colab companion notebook:
Launch the notebook directly in Google Colab to run live code cells or inspect the source repository on GitHub:
View on GitHubContinue exploring related NumPy random features: randn(), random_sample(), randint(), and NumPy Overview.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.