Python Central HubSign in

Data Analytics with Python / Phase 2: Numerical Computing NumPy

NumPy Random Module

Generate random numbers, sample distributions, set seeds for reproducibility, and create synthetic datasets using NumPy.

Why random matters in analytics

Random values help with:

  • Simulation and Monte Carlo experiments
  • Creating synthetic datasets for testing
  • Sampling
  • Bootstrapping

numpy.random supplements Python's built-in random module with functions that generate whole arrays of samples at once. Python's random module only produces one value at a time, so for large samples NumPy is dramatically faster.

Modern NumPy recommends using a Generator object instead of the old global numpy.random functions:

Python
import numpy as np
 
rng = np.random.default_rng(42)
print(rng.integers(1, 10, size=5))
diagram Generating random data with a Generator mermaid
A seeded Generator object produces the same reproducible stream of pseudorandom values every run.

Seeding (reproducibility)

Seeding ensures you get the same results each run โ€” essential for reproducible experiments and tutorials.

Python
import numpy as np
 
rng = np.random.default_rng(123)
print(rng.normal(size=3))

Random integers

Python
import numpy as np
 
rng = np.random.default_rng(7)
arr = rng.integers(low=0, high=100, size=10)
print(arr)

Random floats

Python
import numpy as np
 
rng = np.random.default_rng(7)
arr = rng.random(5)  # uniform in [0, 1)
print(arr)

Normal distribution

Python
import numpy as np
 
rng = np.random.default_rng(7)
arr = rng.normal(loc=0, scale=1, size=5)
print(arr)

Choice (sampling)

Python
import numpy as np
 
rng = np.random.default_rng(7)
categories = np.array(["A", "B", "C"])
print(rng.choice(categories, size=10, replace=True))

Weighted sampling:

Python
import numpy as np
 
rng = np.random.default_rng(7)
values = np.array(["low", "medium", "high"])
probs = [0.6, 0.3, 0.1]
print(rng.choice(values, size=10, p=probs))

Shuffle

Python
import numpy as np
 
rng = np.random.default_rng(7)
arr = np.arange(10)
rng.shuffle(arr)
print(arr)

Synthetic dataset example

Python
import numpy as np
 
rng = np.random.default_rng(0)
 
n = 100
age = rng.integers(18, 60, size=n)
income = rng.normal(loc=60000, scale=15000, size=n)
 
X = np.column_stack([age, income])
print(X.shape)

Check your understanding

quizCheck yourself
  1. Which function does the lesson recommend for modern random number generation?

    Show answer

    B โ€” np.random.default_rng() โ€” default_rng() creates a Generator object, the approach NumPy now recommends over the old global functions.

  2. Why pass a seed such as default_rng(123)?

    Show answer

    B โ€” To get the same random results on every run โ€” Seeding makes the stream reproducible, which matters for experiments and tutorials.

  3. What does rng.random(5) return?

    Show answer

    B โ€” Five floats uniform in [0, 1) โ€” rng.random draws uniform floats from 0 up to but not including 1. rng.normal draws from a normal distribution.

  4. In rng.choice(values, size=10, p=probs) with probs = [0.6, 0.3, 0.1], what does p control?

    Show answer

    B โ€” The probability of picking each value โ€” p gives the weight of each option, so low is picked about 60 percent of the time in the lesson example.

Next

Continue to: Linear Algebra with NumPy to learn dot products, matrix multiplication, and solving systems.

๐Ÿงช Try It Yourself

Exercise 1 โ€“ A Seeded Generator

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 โ€“ Weighted Sampling

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 โ€“ Shuffle In Place

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did