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

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

WBOY
Release: 2024-02-22 12:49:11
forward
757 people have browsed it

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))
Copy after login

Output:

3
0
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template