490 words
2 minutes
NumPy Random Sampling

VI. NumPy Random Sampling (随机抽样)
numpy.random generates pseudo-random numbers (伪随机数) following various distributions (分布). Always set a seed (随机种子) for reproducible experiments. 1. Setting the Seed (设置随机种子)
import numpy as np
np.random.seed(42) # All subsequent random calls are reproducibleNote: The modern recommended way is
rng = np.random.default_rng(42), then use rng.random() etc. (NumPy 1.17+).2. random.rand() — Uniform Distribution (均匀分布)
Core idea: Floats uniformly distributed in [0, 1).
np.random.rand(3) # 1-D: 3 random floatsnp.random.rand(2, 4) # 2-D: shape (2, 4)
# Scale to [a, b]: a + (b - a) * rand()np.random.rand(5) * 10 # uniform in [0, 10)3. random.randn() — Standard Normal Distribution (标准正态分布)
Core idea: Floats from a distribution with mean=0, std=1 (Bell curve / 钟形曲线).
np.random.randn(4) # 1-D: 4 values near 0np.random.randn(3, 3) # 2-D: 3×3 matrix
# Scale to mean=μ, std=σmu, sigma = 5, 2mu + sigma * np.random.randn(100)4. random.randint() — Random Integers (随机整数)
Core idea: Random integers in [low, high) — high is excluded.
np.random.randint(0, 10) # single integer, 0–9np.random.randint(1, 7, size=5) # five dice rollsnp.random.randint(0, 100, size=(3, 4)) # 3×4 matrix5. random.choice() — Sample from Array (从数组中抽取)
Core idea: Randomly pick elements from a 1-D array — with or without replacement (有/无放回).
arr = np.array([10, 20, 30, 40, 50])
np.random.choice(arr, 3) # 3 samples WITH replacementnp.random.choice(arr, 3, replace=False) # 3 samples WITHOUT replacement
# Weighted sampling (带权重抽取)np.random.choice(arr, 3, p=[0.1, 0.2, 0.4, 0.2, 0.1])6. random.normal() — Custom Normal Distribution (自定义正态分布)
Core idea: Generate samples from a normal distribution with any mean (均值) and standard deviation (标准差).
np.random.normal(loc=0, scale=1, size=5) # = randn(5)np.random.normal(loc=170, scale=10, size=1000) # heights in cm7. Other Useful Distributions (其他常用分布)
np.random.uniform(low=1, high=6, size=10) # Continuous uniform (连续均匀)np.random.binomial(n=10, p=0.5, size=5) # Binomial (二项分布)np.random.poisson(lam=3, size=10) # Poisson (泊松分布)np.random.shuffle(arr) # Shuffle in-place (原地打乱)np.random.permutation(arr) # Shuffled copy (打乱副本)8. Quick Comparison Table
| Function (函数) | Distribution | Output Range |
|---|---|---|
rand(*shape) | Uniform | [0, 1) floats |
randn(*shape) | Standard Normal | ≈ [-3, 3] floats |
randint(lo, hi, size) | Discrete Uniform | [lo, hi) integers |
choice(a, n) | Custom array | Elements of a |
normal(μ, σ, size) | Normal | Floats near μ |
uniform(lo, hi, size) | Continuous Uniform | [lo, hi) floats |
💡 One-line Takeaway
Always call
Always call
np.random.seed(n) at the start of experiments for reproducibility, and use replace=False in choice() when sampling without repetition. NumPy Random Sampling
https://lxy-alexander.github.io/blog/posts/numpy/api/06numpy-random-sampling/