Use dist.rvs(size=..., random_state=...) for reproducible simulation. SciPy distributions complement np.random with named families used in tests and Monte Carlo workflows.
Reproducibility
- Pass
random_stateinteger for repeatable labs - Seed NumPy with
np.random.default_rng(seed)when mixing APIs - Document seed in notebooks and tests
Simulation patterns
- Draw samples → compute statistic → compare to theory
- Bootstrap resampling (manual loops on indices)
- Power analysis prototypes before real experiments
Examples
import numpy as np
from scipy import stats
rng = np.random.default_rng(42)
normal = stats.norm.rvs(loc=0, scale=1, size=5, random_state=rng)
uniform = stats.uniform.rvs(0, 1, size=5, random_state=42)
print('normal:', normal)
print('uniform:', uniform)
Important interview questions and answers
- Q: rvs vs np.random.normal?
A: Both work; stats.rvs ties sampling to named distribution objects used in inference. - Q: Why random_state?
A: Reproducible lessons, tests, and debugging—same seed → same draws.
Self-check
- How do you draw 10 samples from Normal(0,1) reproducibly?
- Name one simulation use case for rvs.
Tip: Pass random_state in lessons and tests so playground output matches MCQ explanations.
Interview prep
- random_state?
Reproducible draws for tests and teaching.
- rvs?
Sample from named distribution—ties to theory and simulation.