Random numbers from a Generator
Modern NumPy uses a Generator object created by np.random.default_rng(). The generator has methods for common distributions: integers draws whole numbers, normal draws from a bell curve, and permutation shuffles a sequence.
Python
Dice, sensor noise, and a shuffled range
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
- rng.integers(low, high, size) includes low and excludes high, just like range().
- rng.normal(loc, scale, size) uses the mean and standard deviation you provide.
- rng.permutation(values) returns a shuffled copy of the sequence.
Seeds make a run reproducible
A seed initializes the generator's internal state. Two generators created with the same seed and asked for the same sequence produce the same values. That does not make the values predictable in a security sense; it makes experiments repeatable.
Python
Same seed, same starting stream
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
The exact numbers can change across NumPy versions, so do not build tests around a hard-coded stream. Test the properties you care about, such as shape, range, or a seeded comparison within the same run.
Elementwise multiplication versus matrix products
The * operator multiplies matching positions. The @ operator performs a matrix product, where each output value is a dot product of a row from the left and a column from the right. They answer different questions and are not interchangeable.
Python
The same two matrices, two operations
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
For A @ B, the inner dimensions must agree. A has shape (2, 2) and B has shape (2, 2), so the result is (2, 2). If A were (4, 2) and B were (2, 3), the inner 2s would match and the result would be (4, 3).
Solving a linear system
A system such as A @ x = b asks for the vector x that satisfies every equation. np.linalg.solve(A, b) computes that solution directly. It is more stable and usually faster than multiplying by np.linalg.inv(A).
Python
Find x, then verify the original equation
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
The verification A @ x should reproduce b, up to small floating-point differences. Keep that check in your notebook or test: it catches shape mistakes and accidental elementwise multiplication.
A small seeded simulation
Randomness is useful for simulating measurements. The example below draws one hundred readings from a normal distribution, then summarizes them with the same aggregations from the previous lesson.
Python
Simulate, then summarize
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
Change the seed and the sample changes. Keep the seed fixed when you want a stable demonstration, and remove the fixed seed when you want a genuinely new sample each run.
Practice
Make it stick
Run the tests as often as you like. Hints and the solution are available when you need them.
Exercise
Solve A @ x = b for x using NumPy's linear algebra function. Keep x as a one-dimensional array.
Your solution
ReadyResults
Run the tests to check your solution. The first run downloads Python; later runs reuse the session.
Quiz
Check your understanding
Lesson progress