Home  >  Article  >  Backend Development  >  Why do I get different random numbers using the same seed?

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

WBOY
WBOYforward
2024-02-22 12:49:11683browse

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

Question content

I am using a 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))

Output:

3
0

Why is the output different?


Correct answer


numpy.random.randint and numpy.random.seed use the old random API, which The underlying implementation is completely different. numpy.random.default_rng Create a Generator object, this is the new API.

These two APIs are actually two completely independent RNG libraries that happen to be in the same namespace. Even with the same seed, the output won't match.

The above is the detailed content of Why do I get different random numbers using the same seed?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete