Chapter 1: Random Numbers in NumPy

Random Numbers in NumPy — written as if I’m your patient teacher sitting next to you, showing examples on the screen, explaining every important detail, giving realistic use-cases, warning about common mistakes, and helping you build good habits from the beginning.

Let’s imagine we’re working together in a Jupyter notebook. Ready? 😊

Python

1. Why NumPy random instead of Python’s random?

Many beginners start with Python’s built-in random module.

But in serious numerical/scientific/ML work you should almost always use numpy.random.

Main reasons:

  • Much faster when generating thousands/millions of numbers
  • Returns NumPy arrays directly (ready for math, slicing, broadcasting)
  • Much larger variety of distributions (normal, binomial, poisson, beta, gamma, multivariate…)
  • Proper multi-dimensional support out of the box
  • Consistent seeding behavior across different functions
  • Better integration with the rest of the NumPy ecosystem

Golden rule #1 (write it somewhere):

If you’re doing anything numerical/scientific/ML/data analysis → use numpy.random — not random, not secrets, not anything else.

2. The MOST important habit: Always control the seed

Python

Why is this so important?

  • Makes experiments reproducible
  • Makes debugging much easier
  • Allows others to get exactly the same results
  • Extremely helpful when teaching, writing tutorials, comparing models

Quick demonstration:

Python

Teacher tip: During learning/experimenting → always put a fixed seed at the top of your notebook/script. When you go to production or want true randomness → remove/comment out the seed line.

3. The most frequently used random functions (you’ll use these 90% of the time)

A. np.random.rand() — Uniform random numbers [0, 1)

Python

B. np.random.randn() — Standard normal (Gaussian) distribution

Mean = 0, standard deviation = 1

Python

Quick comparison table students should remember:

Function Range / Distribution Typical shape example Most common use case
rand() [0, 1) uniform rand(1000, 30) Features, dropout masks, probabilities
randn() ~ Normal(0, 1) randn(512, 512, 3) Weights init, noise, synthetic data
randint() integers [low, high) randint(1, 7, size=100) Dice, labels, indices, pixel values

C. np.random.randint(low, high, size=…) — Random integers

Python

D. np.random.uniform(low, high, size=…) — Uniform in custom range

Python

E. np.random.normal(loc, scale, size=…) — Custom normal distribution

Python

4. Very common realistic use cases (you will write these many times)

Use case 1 – Synthetic training data

Python

Use case 2 – Adding realistic noise to images

Python

Use case 3 – Random train/val/test split (manual)

Python

Use case 4 – Random sampling / bootstrapping

Python

Summary – Your Quick Reference Table

Function What you get Typical shape Typical seed usage
rand() Uniform [0,1) rand(1000,20) Yes
randn() Standard normal (μ=0, σ=1) randn(512,512,3) Yes
randint(low,high) Integers [low, high) randint(1,101,10000) Yes
uniform(a,b) Uniform [a,b) uniform(0,100,500) Yes
normal(μ,σ) Normal distribution μ,σ normal(170,10,10000) Yes
choice() Sample from given array choice(names, 200) Yes
shuffle() Shuffle array in place shuffle(dataset) Yes (before)

Final teacher advice (very important)

Always start your random-related work with:

Python

This tiny habit will save you many hours of confusion later.

Where do you want to go next?

  • More exotic distributions (poisson, binomial, beta, gamma…)
  • Randomness in machine learning (dropout, weight init, data augmentation)
  • Common bugs & misunderstandings with random numbers
  • Mini-project: generate synthetic dataset + add noise + visualize
  • Reproducibility tricks across multiple files/notebooks

Just tell me what feels most interesting or useful for you right now! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *