NumPy Random

NumPy Random — written as if we are sitting together at a table, I’m showing you examples on my screen, explaining every important concept, showing realistic use cases, warning about common traps, and giving you plenty of runnable code examples.

Let’s go step by step — just like a real class.

Python

Why do we need NumPy’s random module?

numpy.random is the standard way to generate random numbers in almost all scientific, machine learning, data analysis, simulation, and testing work done in Python.

Why not use Python’s built-in random module?

  • np.random is much faster for generating large arrays
  • It produces ndarrays directly (ready for math, slicing, reshaping…)
  • It has far more distributions (normal, uniform, binomial, poisson, beta, gamma…)
  • It supports seeding for perfect reproducibility
  • It works beautifully with vectorization and broadcasting

Golden rule #1 (write this down):

Whenever you need random numbers for data science, ML, simulations, or testing → use numpy.random — not random.

Step 1 – The most important habit: Setting the random seed

Python

Every time you run your code with the same seed → you get exactly the same random numbers.

This is extremely important when:

  • Debugging
  • Comparing experiments
  • Sharing code
  • Writing tests
  • Teaching / tutorials

Example – without seed vs with seed

Python

Teacher tip: Put np.random.seed(42) (or any fixed number) at the top of your notebooks/scripts when you start learning or experimenting. Later, remove it (or change it) when you want truly random behavior.

Step 2 – Most commonly used random functions

1. np.random.rand() – Uniform random numbers in [0, 1)

Python

Realistic use case – creating synthetic features

Python

2. np.random.randn() – Standard normal (Gaussian) distribution

Mean = 0, standard deviation = 1

Python

Quick comparison – rand vs randn

Python

3. np.random.randint() – Random integers

Python

4. np.random.uniform() – Uniform with custom range

Python

5. np.random.normal() – Normal with custom mean & std

Python

6. np.random.choice() – Sampling from existing array

Python

7. np.random.shuffle() – Shuffle in place

Python

Note: shuffle modifies the array in place — no return value.

Step 3 – Realistic & common use cases (you will write these often)

Use case 1 – Creating synthetic training data

Python

Use case 2 – Train / validation / test split (manual)

Python

Use case 3 – Random missing values simulation

Python

Use case 4 – Random image-like noise

Python

Summary – Quick Reference Table

Function Distribution / Behavior Typical shape example Common seed usage
rand() Uniform [0, 1) rand(1000, 20) Yes
randn() Standard normal (μ=0, σ=1) randn(784, 10) Yes
randint(low, high) Integers [low, high) randint(1, 7, size=100) Yes
uniform(low, high) Uniform [low, high) uniform(18, 32, 365) Yes
normal(loc, scale) Normal μ=loc, σ=scale normal(100, 15, 1000) Yes
choice() Sample from given array choice(fruits, 50) Yes
shuffle() Shuffle array in place shuffle(deck) Yes

Final teacher advice

Always start your notebooks/experiments with:

Python

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

Where would you like to go next?

  • Random distributions in more depth (binomial, poisson, beta…)
  • Randomness in machine learning (data splitting, weight init, dropout…)
  • Common bugs when using random numbers
  • Mini-project: simulate data + add noise + clean it
  • Reproducibility across multiple runs / files

Just tell me what you want to focus on now! 😊

You may also like...

Leave a Reply

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