pythonpython-3.xrandomseed

How to introduce seed in np.random.random?


I need to create a number of points in a square [0,1]x[0,1].

To obtain an array of coordinates, I used:

N_fixed_points = 10
selection = np.random.random((N_fixed_points,2))

which gives me a proper result.

But I would like to reproduce the same selection output and I know that I should use a random number generator such as:

rng = np.random.default_rng(seed=1949)

What is the simplest way to update the selection line of code with rng?


Solution

  • You just call the RNG’s own random method.

    import numpy as np
    rng = np.random.default_rng(1949)
    selection = rng.random((N_fixed_points, 2))