2025, Nov 13 15:00

How to Generate Reproducible Random Coordinates in [0,1] x [0,1] Using NumPy's Seeded default_rng

Learn to generate reproducible random coordinates in a unit square with NumPy: create a seeded default_rng and draw via .random for deterministic sampling.

Generating reproducible random coordinates in a unit square is a routine task, but it quickly turns into a headache if the random stream isn’t controlled. The goal here is straightforward: sample points in [0, 1] x [0, 1] and be able to recreate the exact same array later.

Problem setup

You can sample an array of points with a direct call to the random API. That works, but it doesn’t lock in the sequence for future runs.

import numpy as np
count_pts = 10
picked = np.random.random((count_pts, 2))

This produces a valid set of coordinates, each row being a point inside the unit square.

What’s going on

The sampling above relies on the module-level generator, which is fine for a quick draw but doesn’t address reproducibility. To repeat the exact same output, you need to work with a seeded generator instance and use its own method to draw numbers.

Fix: use the generator’s random method

Create a generator with a seed and then call its random method to produce the same shape of data. That’s the whole change.

import numpy as np
count_pts = 10
prng = np.random.default_rng(1949)
picked = prng.random((count_pts, 2))

With this, the array of points is reproduced consistently for the same seed.

Why this matters

Deterministic sampling makes debugging and verification predictable. When the random stream is anchored to a seed and you draw from the same source, results can be rechecked and compared without guesswork.

Takeaway

When you need reproducible random coordinates, create a seeded generator and draw via its random method. Keep the shape the same as in the non-deterministic version, and you’ll get identical arrays for the same seed every time.