Skip to content
Learn Netverks

Lesson

Step 11/36 31% through track

numpy-random

Random numbers with NumPy

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Random numbers with NumPy: NumPy ndarray operations, vectorization, and numerical patterns used across the Python scientific stack.

Teams apply Random numbers with NumPy in every serious NumPy project—skipping it leaves blind spots in analysis and reviews.

You will apply Random numbers with NumPy in contexts like: Monte Carlo experiments, synthetic data, and train/validation shuffles.

Read the narrative, run `import numpy as np` snippets in the playground (install NumPy with pip if the runner lacks it), tweak shapes and dtypes, and complete MCQs.

When you can explain the previous lesson's ideas in your own words.

Modern NumPy uses np.random.default_rng(seed) to generate reproducible pseudo-random samples for simulation, train/test splits, and data augmentation.

Generator API

import numpy as np
rng = np.random.default_rng(42)
ints = rng.integers(0, 10, size=5)
norm = rng.normal(0, 1, size=5)
print(ints, norm)

Common distributions

  • integers(low, high, size) — discrete uniform
  • normal(loc, scale, size) — Gaussian
  • uniform(low, high, size) — continuous uniform
  • choice(a, size, replace) — sample from array
  • shuffle — in-place permutation

Reproducibility

Same seed → same sequence. Set seed once per experiment; document it in notebooks per data science reproducibility habits.

Important interview questions and answers

  1. Q: Why not legacy np.random.rand?
    A: Generator API has better statistical properties and clearer interface.
  2. Q: choice with replace=False?
    A: Samples without replacement—like a shuffle pick.

Self-check

  1. Generate 5 standard normal samples with seed 0.
  2. What method samples integers in a range?

Tip: Use default_rng(seed) and document seeds in notebooks.

Interview prep

default_rng?

Modern Generator API—preferred over legacy global random state.

Seed purpose?

Same seed → same sequence for reproducible tests.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Seed reproducibility?
  • rng vs RandomState?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump