Master NumPy From Scratch

Learn NumPy fundamentals, arrays, indexing, slicing, data types, array operations, broadcasting, mathematical functions, statistics, random numbers, linear algebra, file handling, and practical projects through structured lessons.

COURSE SYLLABUS
1. NumPy Introduction and Installation
Introduction to NumPy NumPy Installation and Setup NumPy Environment and IDE Setup First NumPy Program
2. NumPy Arrays and Array Creation
NumPy Array Fundamentals Array Creation Functions Zeros, Ones and Identity Arrays Range and Linspace Arrays
3. NumPy Array Indexing and Slicing
One-Dimensional Array Indexing Array Slicing Techniques Two-Dimensional Array Indexing Boolean and Fancy Indexing
4. NumPy Data Types and Attributes
NumPy Data Types Array Shape, Size and Dimensions Data Type Conversion and Casting Array Inspection and Attributes
5. NumPy Array Shape and Reshaping
Reshape and Flatten Arrays Transpose and Axis Operations Stacking and Splitting Arrays Expand and Squeeze Dimensions
6. NumPy Array Operations and Broadcasting
Arithmetic Array Operations Comparison and Logical Operations Broadcasting Fundamentals Universal Functions and Vectorization
7. NumPy Mathematical and Statistical Functions
Mathematical Functions Aggregate and Statistical Functions Minimum, Maximum and Percentiles Rounding and Trigonometric Functions
8. NumPy Filtering and Sorting
Array Filtering and Conditions Sorting and Searching Arrays Unique Values and Set Operations Where, Select and Nonzero Functions
9. NumPy Copies and Views
Array Copies and Views Shallow and Deep Copy Concepts Memory Layout and Contiguity Array Iteration and Memory Efficiency
10. NumPy Random Number Generation
Random Generator Fundamentals Random Integers and Sampling Probability Distributions Random Seeds and Simulation
11. NumPy Linear Algebra
Vectors and Matrices Matrix Multiplication and Dot Product Determinants and Matrix Inverse Eigenvalues and Linear Equations
12. NumPy File Input and Output
Importing and Exporting Text Files CSV Data with NumPy Binary Files and NPY Format NPZ Files and Data Persistence
13. NumPy Practical Projects
Student Marks Analysis Project Sales Data Analysis Project Random Simulation Project Image Array Processing Project
14. NumPy Practice and Assessment
NumPy Beginner Practice NumPy Intermediate Exercises NumPy Advanced Challenges Final NumPy Assessment

Random Seeds and Simulation

Learn how to control random number generation using seeds, reproduce NumPy experiments, create independent random generators, and simulate real-world events using random sampling and Monte Carlo methods. Explore reproducibility, probability estimation, vectorized simulation, and practical numerical experiments with detailed examples.

1. Learning Objectives

By the end of this lesson, you will be able to:

  • Understand pseudo-random number generation.
  • Explain the purpose of a random seed.
  • Create random generators using np.random.default_rng().
  • Generate reproducible random sequences.
  • Understand how generator state changes after each call.
  • Create separate random generators for different tasks.
  • Understand the difference between the modern and legacy random APIs.
  • Simulate coin tosses and dice rolls.
  • Estimate probabilities using repeated random experiments.
  • Apply Monte Carlo simulation to estimate numerical values.
  • Use NumPy vectorization for efficient simulation.

2. Introduction to Random Seeds

Random numbers are useful in scientific computing, statistical analysis, machine learning, testing, and simulation. However, some experiments need to produce the same results whenever the code is executed under the same conditions.

A random seed is an initial value used to initialize a pseudo-random number generator. Using a seed allows us to reproduce a generated sequence when we recreate the generator with the same seed and repeat the same operations.

import numpy as np rng = np.random.default_rng(42) numbers = rng.integers(1, 101, size=5) print(numbers)

In this example, 42 is the seed. The generator produces five integers from 1 through 100 because the upper bound of integers() is exclusive by default.

Tip: A seed is not the list of random numbers. It initializes the generator, which then produces a sequence as random values are requested.

3. Understanding Pseudo-Random Numbers

Computers commonly generate pseudo-random numbers using mathematical algorithms. These values appear random, but they are produced from an internal generator state.

When a generator is initialized with a particular seed, its starting state is determined by that seed. Each random operation advances the state and generates the next requested values.

import numpy as np rng = np.random.default_rng(10) values = rng.random(5) print(values)

The random() method generates floating-point values in the interval from 0 inclusive to 1 exclusive.

Important Characteristics

  • A seed initializes a generator.
  • A generator produces values according to its algorithm.
  • The generator maintains an internal state.
  • Requesting more values advances that state.
  • Reinitializing the generator with the same seed can reproduce the sequence.
Important: NumPy's random generators are designed for numerical work and simulation, not for generating cryptographically secure passwords, authentication tokens, or security keys.

4. Creating Reproducible Random Sequences

Reproducibility is important when testing programs, comparing algorithms, investigating errors, and verifying numerical experiments. Two generators initialized with the same seed and used with the same sequence of operations produce matching outputs.

import numpy as np rng1 = np.random.default_rng(100) rng2 = np.random.default_rng(100) values1 = rng1.integers(1, 101, size=5) values2 = rng2.integers(1, 101, size=5) print("First sequence:", values1) print("Second sequence:", values2) print("Sequences match:", np.array_equal(values1, values2))

Output:

First sequence: [77 83 4 59 32] Second sequence: [77 83 4 59 32] Sequences match: True

Both generators start with the same seed and perform the same operation. Therefore, the generated arrays match.

Best practice: Use a fixed seed when you need reproducible experiments. Record the seed and the relevant simulation settings so that the experiment can be repeated.

5. Comparing Different Seeds

Different seeds generally initialize a random generator to different states. As a result, they usually produce different random sequences.

import numpy as np rng1 = np.random.default_rng(10) rng2 = np.random.default_rng(20) values1 = rng1.integers(1, 51, size=6) values2 = rng2.integers(1, 51, size=6) print("Seed 10:", values1) print("Seed 20:", values2)

The generated arrays will generally differ because the generators were initialized differently.

However, different seeds do not guarantee that every generated number will be different. Individual values can match by chance.

Remember: Use seeds to initialize and reproduce random experiments. Do not assume that a different seed guarantees completely different values throughout every generated array.

6. Understanding Generator State

A random number generator maintains internal state. Each call generally advances the state, so repeated calls on the same generator continue the sequence rather than restarting it.

import numpy as np rng = np.random.default_rng(42) first_batch = rng.integers(1, 11, size=5) second_batch = rng.integers(1, 11, size=5) print("First batch:", first_batch) print("Second batch:", second_batch)

The first call generates five integers. The second call continues from the generator's updated state and generates the next five integers.

Restarting a Sequence

To start the same sequence again, create a new generator using the same seed.

import numpy as np rng1 = np.random.default_rng(42) first_run = rng1.integers(1, 11, size=5) rng2 = np.random.default_rng(42) second_run = rng2.integers(1, 11, size=5) print("First run:", first_run) print("Second run:", second_run) print(np.array_equal(first_run, second_run))

Output:

True

Both generators start from the same seed and perform the same operation, so their first batches match.

7. Creating Separate Random Generators

A program may contain multiple experiments. For example, one part of a program may simulate student marks while another part simulates attendance. Separate generators help keep these tasks organized.

import numpy as np marks_rng = np.random.default_rng(101) attendance_rng = np.random.default_rng(202) marks = marks_rng.integers(40, 101, size=5) attendance = attendance_rng.integers(70, 101, size=5) print("Marks:", marks) print("Attendance:", attendance)

The marks generator and attendance generator maintain separate states. Requesting additional attendance values does not advance the marks generator.

Using Separate Generators in a Project

import numpy as np sales_rng = np.random.default_rng(11) customer_rng = np.random.default_rng(22) daily_sales = sales_rng.integers(1000, 5001, size=7) daily_customers = customer_rng.integers(20, 101, size=7) print("Daily sales:", daily_sales) print("Daily customers:", daily_customers)

Separate generators make it easier to manage different random processes in the same application.

Tip: For advanced parallel simulations, NumPy provides SeedSequence and supported generator-spawning techniques to help create separate random streams.

8. Modern Random API vs Legacy API

NumPy provides a modern random generator interface and older random functions. For new projects, the recommended starting point is np.random.default_rng().

Feature Modern API Legacy API
Initialization np.random.default_rng(seed) np.random.seed(seed)
Random generation rng.random() np.random.random()
State management Generator object with its own state Shared global random state
Recommended for new code Yes Generally avoid for new code

Modern Approach

import numpy as np rng = np.random.default_rng(42) values = rng.random(5) print(values)

Legacy Approach

import numpy as np np.random.seed(42) values = np.random.random(5) print(values)

The modern approach stores the generator in a variable. You can pass that generator to different functions, making the random state explicit and easier to manage.

Important: The modern and legacy APIs should not be expected to produce identical sequences from the same numeric seed.

9. Introduction to Simulation

Simulation is the process of modeling a situation by generating possible outcomes and analyzing the results. Random simulation is useful when outcomes involve uncertainty or when repeated experiments can help estimate a quantity.

Examples of simulation include:

  • Simulating coin tosses.
  • Simulating dice rolls.
  • Estimating the probability of an event.
  • Modeling customer demand or daily sales.
  • Generating sample student marks for testing.
  • Estimating numerical values through Monte Carlo methods.
import numpy as np rng = np.random.default_rng(42) # 0 represents tails and 1 represents heads. tosses = rng.integers(0, 2, size=20) heads = np.count_nonzero(tosses == 1) tails = np.count_nonzero(tosses == 0) print("Tosses:", tosses) print("Heads:", heads) print("Tails:", tails)

The code simulates 20 coin tosses. The resulting counts show the observed number of heads and tails in this particular experiment.

10. Simulating Dice Rolls

A fair six-sided die has six possible outcomes: 1, 2, 3, 4, 5, and 6. We can simulate many rolls using rng.integers().

import numpy as np rng = np.random.default_rng(50) rolls = rng.integers(1, 7, size=1000) print("First 20 rolls:", rolls[:20]) print("Average roll:", rolls.mean()) for face in range(1, 7): count = np.count_nonzero(rolls == face) print(f"Face {face}: {count}")

The upper bound is 7 because the default upper-bound behavior excludes that value. This makes 1 through 6 the possible outcomes.

With many rolls, the observed frequency of each face will generally approach its theoretical probability of one-sixth. The counts will not necessarily be equal.

Learning Point: A simulation provides observed results from a finite number of trials. These results can vary from run to run when the seed or generated sequence changes.

11. Estimating Probability Through Simulation

Probability can be estimated by repeating an experiment many times and calculating the proportion of trials in which a particular event occurs.

For a fair die, the theoretical probability of rolling a six is one-sixth. We can estimate this probability using repeated simulated rolls.

import numpy as np rng = np.random.default_rng(123) number_of_rolls = 100_000 rolls = rng.integers(1, 7, size=number_of_rolls) number_of_sixes = np.count_nonzero(rolls == 6) estimated_probability = number_of_sixes / number_of_rolls theoretical_probability = 1 / 6 print("Number of sixes:", number_of_sixes) print("Estimated probability:", estimated_probability) print("Theoretical probability:", theoretical_probability)

The estimated probability is the observed number of sixes divided by the total number of rolls.

The estimate will generally be close to one-sixth when the number of independent trials is large, but it is not guaranteed to equal the theoretical probability exactly.

12. Monte Carlo Simulation

Monte Carlo simulation uses random sampling to estimate numerical quantities. It is especially useful when a direct calculation is difficult or when we want to approximate a result through repeated experiments.

Example: Estimating the Value of Pi

Consider a square whose coordinates range from -1 to 1 on both axes. A circle of radius 1 is centered inside the square. Random points are generated uniformly within the square.

A point lies inside the circle when x² + y² <= 1. The ratio of the circle's area to the square's area is π/4. Therefore, the fraction of sampled points inside the circle can be multiplied by four to estimate π.

import numpy as np rng = np.random.default_rng(42) number_of_points = 100_000 x = rng.uniform(-1, 1, size=number_of_points) y = rng.uniform(-1, 1, size=number_of_points) inside_circle = x**2 + y**2 <= 1 inside_count = np.count_nonzero(inside_circle) pi_estimate = 4 * inside_count / number_of_points print("Points inside circle:", inside_count) print("Total points:", number_of_points) print("Estimated pi:", pi_estimate) print("Actual pi:", np.pi) print("Absolute error:", abs(pi_estimate - np.pi))

The generated points are checked against the circle condition. The number of points inside the circle is used to calculate the estimate.

Tip: Try changing the number of points to 1,000, 10,000, and 500,000. Compare the estimates and their absolute errors to see how sample size affects this simulation.
Important: Monte Carlo simulation produces an approximation. Increasing the number of points often improves the estimate, but it does not guarantee that every individual run will be more accurate.

13. The Law of Large Numbers

The law of large numbers describes how the average of repeated independent observations tends to approach the expected value under appropriate conditions as the number of observations increases.

For a fair six-sided die, the theoretical expected value is 3.5. We can compare the average obtained from different sample sizes.

import numpy as np rng = np.random.default_rng(123) sample_sizes = [10, 100, 1_000, 10_000, 100_000] for size in sample_sizes: rolls = rng.integers(1, 7, size=size) print( f"Trials: {size:>6}, " f"average: {rolls.mean():.4f}" ) print("Theoretical average:", 3.5)

The averages will often approach 3.5 as the number of observations increases. However, the average does not have to move closer to 3.5 at every step.

What to Observe

  • Small samples can show substantial random variation.
  • Larger samples often produce more stable averages.
  • The sample average can still differ from the theoretical value.
  • Simulation results depend on the generated observations.

14. Simulating Student Marks

Random simulation can be used to create synthetic datasets for testing data analysis programs. Consider three fictional students at CIIT TRANING INSTITUTE: Samadhan, Pradnya, and Samiksha.

We will simulate their marks across four subjects and calculate their total and average marks.

import numpy as np rng = np.random.default_rng(77) students = np.array([ "Samadhan", "Pradnya", "Samiksha" ]) subjects = np.array([ "Python", "NumPy", "Statistics", "Projects" ]) marks = rng.integers(35, 101, size=(3, 4)) print("Students:", students) print("Subjects:", subjects) print("Marks:") print(marks) total_marks = marks.sum(axis=1) average_marks = marks.mean(axis=1) for i, student in enumerate(students): print( f"{student}: " f"Total = {total_marks[i]}, " f"Average = {average_marks[i]:.2f}" ) print("Subject averages:", marks.mean(axis=0))

The array has three rows and four columns. Each row represents one student, and each column represents one subject.

The parameter axis=1 calculates a result for each student across the subjects. The parameter axis=0 calculates an average for each subject across the students.

Note: These are synthetic marks generated for learning purposes. They are not actual student results.

15. Vectorized Random Simulation

Vectorization means performing operations on entire arrays instead of processing every element through a Python loop. NumPy's array-based operations make it convenient to generate and analyze large collections of random values.

Generating Normally Distributed Values

import numpy as np rng = np.random.default_rng(88) values = rng.normal( loc=50, scale=10, size=100_000 ) print("Mean:", values.mean()) print("Standard deviation:", values.std()) print("Minimum:", values.min()) print("Maximum:", values.max())

The normal() method generates samples from a normal distribution. The loc parameter specifies the distribution mean, and scale specifies its standard deviation.

The sample mean and sample standard deviation will generally be near the specified parameters, but they will not necessarily match them exactly.

Generating Multiple Experiments

import numpy as np rng = np.random.default_rng(90) experiments = rng.integers( 1, 7, size=(5, 10) ) print("Experiment results:") print(experiments) experiment_averages = experiments.mean(axis=1) print("Average for each experiment:") print(experiment_averages)

This example creates five experiments, each containing ten simulated dice rolls. Each row represents one experiment, and axis=1 calculates the average of each row.

16. Common Mistakes and Best Practices

Common Mistakes

  • Expecting repeated calls on the same generator to restart the sequence.
  • Assuming that different seeds guarantee different individual values.
  • Forgetting that the upper bound of integers() is exclusive by default.
  • Using global random state unnecessarily in new programs.
  • Treating simulated results as exact theoretical answers.
  • Assuming every larger simulation must produce a more accurate estimate.
  • Assuming that two arbitrary seeds automatically guarantee statistically independent streams.
  • Using NumPy random generation for security-sensitive tasks.

Best Practices

  • Prefer np.random.default_rng() in new NumPy code.
  • Use a fixed seed when reproducibility is required.
  • Record the seed, sample size, and relevant simulation settings.
  • Use separate generators to organize separate tasks.
  • Use vectorized operations when they make the simulation clearer and more efficient.
  • Choose a sample size appropriate for the problem.
  • Interpret simulation estimates as approximations when appropriate.

17. Practice Exercises

  1. Create two generators with seed 500 and verify that both produce matching arrays of random integers.
  2. Generate 500 random integers between 1 and 100. Calculate the mean, median, minimum, and maximum.
  3. Simulate 5,000 coin tosses and calculate the observed proportion of heads.
  4. Simulate 10,000 dice rolls and calculate the observed frequency of each face.
  5. Estimate the probability of rolling a six using 100,000 simulated dice rolls.
  6. Estimate pi using 250,000 random points and compare the estimate with np.pi.
  7. Generate simulated marks for Samadhan, Pradnya, and Samiksha across five subjects. Calculate each student's average.
  8. Create separate generators for simulated sales and customer counts.
  9. Run a simulation with different sample sizes and explain how the estimated average changes.
  10. Explain why a random seed is useful when debugging or testing a numerical program.

Summary

In this lesson, we learned how random seeds initialize pseudo-random number generators and how NumPy's np.random.default_rng() provides a modern approach to random number generation. We explored reproducible sequences, different seeds, generator state, and separate generators for different tasks.

We also simulated coin tosses, dice rolls, student marks, and normally distributed values. Monte Carlo simulation demonstrated how random sampling can estimate numerical quantities such as pi and probabilities. The law of large numbers explained why sample averages often approach theoretical expectations as sample sizes grow.

Remember to use a fixed seed when reproducibility is important, prefer the modern random generator API for new code, and interpret simulation results according to their sample size and limitations.