pythonnumpyrandom-seed

Why do I get different random numbers with the same seed?


I am using the numpy random number generator with the following MWE:

import numpy as np
np.random.seed(40)
print(np.random.randint(-3, 4))
rng = np.random.default_rng(seed=40)
print(rng.integers(-3, 4))

This outputs:

3
0

Why are the outputs different?


Solution

  • numpy.random.randint and numpy.random.seed use the old random API, with an entirely different implementation under the hood. numpy.random.default_rng creates a Generator object, which is the new API.

    The two APIs are effectively two entirely separate RNG libraries that happen to live in the same namespace. Output isn't expected to match, even with the same seed.