/Curriculum/BeginnerRandom Generation

07. NumPy Random ModuleBeginner

Generate random integers, uniform floats, standard normal distributions, and lock seeds.

15 mins

Concept Overview

The `np.random` module provides high-speed pseudo-random number generation for simulations, statistical sampling, and machine learning weight initialization.

Hardware Mental Model

A random seed is the bookmark in an infinite mathematical phonebook of deterministic numbers. Setting the same seed reads the exact same pages every time.

Key Concepts (1)Click snippet to load in editor

`rand()` generates uniform floats in [0, 1). `randint(low, high)` generates discrete integers. `randn()` samples standard normal Gaussian distribution (mean=0, std=1).

np.random.seed(42); arr = np.random.randint(1, 10, size=5)

Complete NumPy Random Functions Matrix

All 10 random generation functions compared at a glance
np.random.rand(d0, d1, ...)Floats (Dimensions)

Continuous uniform probability distribution across unit interval

Returns:Uniform Floats [0.0, 1.0)
np.random.randn(d0, d1, ...)Bell Curve

Standard normal distribution bell curve (can produce negative values)

Returns:Gaussian Normal (Mean=0, Std=1)
np.random.randint(low, high, size)Integers

Sample discrete integers (high endpoint excluded)

Returns:Discrete Integers [low, high)
np.random.random(size)Tuple Shape

Continuous uniform float sampling accepting a shape tuple as argument

Returns:Uniform Floats [0.0, 1.0)
np.random.choice(a, size)Discrete Sampling

Sample random items with or without replacement from an input vector

Returns:Categorical Elements
np.random.shuffle(arr)In-Place Mutate

Modifies existing array directly in memory without returning a new object

Returns:In-Place Permutation
np.random.permutation(x)Returns New Copy

Returns a fresh randomly permuted sequence or array copy

Returns:New Shuffled Copy
np.random.uniform(low, high, size)Bounded Range

Continuous floats sampled uniformly between custom low & high bounds

Returns:Custom Range Floats
np.random.normal(loc, scale, size)Custom Gaussian

Normal distribution with custom center loc (μ) and spread scale (σ)

Returns:Gaussian (Custom Mean, Std)
np.random.seed(42)Seed Lock

Ensures generated random values are 100% identical on every run

Returns:Locks PRNG State

⚠ Common Mistakes & Pitfalls

Avoid these frequent beginner syntax and logic traps
❌ Incorrect:np.random.randint(1, 10) # generates 1 scalar instead of array
✓ Correct:np.random.randint(1, 10, size=5)

Provide `size=N` to generate an array of numbers instead of a single integer.

Pro Tip: Use `size=(rows, cols)` to generate multi-dimensional random arrays directly.

📌 Quick Revision

Core takeaway points from this topic
`np.random.seed(x)`: Guarantees reproducible sequences.
`np.random.rand(d0, d1)`: Uniform floats in [0.0, 1.0).
`np.random.randint(low, high, size)`: Uniform discrete integers.
`np.random.randn(d0, d1)`: Standard Gaussian distribution.
Editor: 07. NumPy Random Module
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state

🎯 Try It Yourself: Practice Challenge

Hands-on Mode

Solve the objective below using NumPy vectorized syntax

Objective:Generate Seeded Random Integers

Set seed to 42, generate 5 random integers between 1 and 20 with `size=5`, and print them.

Expected Output Target:[ 7 15 11 8 7]
Practice Workspace
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state