Skip to content
LearnPY
NumPy

Randomness and linear algebra

Generate reproducible random data with a seeded Generator, then use matrix products and solve to work with systems of equations.

10 min readbeginnernumpy

By the end

  • Create reproducible random values with np.random.default_rng.
  • Choose between elementwise multiplication and the @ matrix product.
  • Check matrix shapes before multiplying.
  • Solve a linear system without computing an explicit inverse.

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

Ready
Loading editor…

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

Ready
Loading editor…

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

Ready
Loading editor…

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

Ready
Loading editor…

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

Ready
Loading editor…

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

Ready
Loading editor…

Results

Run the tests to check your solution. The first run downloads Python; later runs reuse the session.

Quiz

Check your understanding

01What does A @ B compute?
02Why do two default_rng(42) generators produce the same starting values?
03What is the shape of a (4, 2) matrix multiplied by a (2, 3) matrix?

Lesson progress