According to NumPy's numpy.random.seed() documentation:
This is a convenience, legacy function.
The best practice is to not reseed a BitGenerator, rather to recreate a new one. This method is here for legacy reasons. This example demonstrates best practice.
However, I noticed that the results from recreating a bit generator are not reproducible. Rather, reseeding the bit generator gives reproducible results. Why is this the case? What am I doing wrong?
Also, their results differ. Why is this so? Isn't the same Mersenne Twister (MT) algorithm used?
My script for reproducing my observation is shown below.
import numpy as np
from numpy.random import MT19937
from numpy.random import RandomState, SeedSequence
import matplotlib.pyplot as plt
seed=123456789
# Reseed a BitGenerator
np.random.seed(seed)
r1 = np.random.random_integers(1, 6, 1000)
np.random.seed(seed)
r2 = np.random.random_integers(1, 6, 1000)
# Recreate a BitGenerator
rs = RandomState(MT19937(SeedSequence(seed)))
c1 = np.random.random_integers(1, 6, 1000)
rs = RandomState(MT19937(SeedSequence(seed)))
c2 = np.random.random_integers(1, 6, 1000)
# Visualise results
fig, axes = plt.subplots(1, 2)
axes[0].hist(r1, 11, density=True)
axes[0].hist(r2, 11, density=True)
axes[0].set_title('Reseed a BitGenerator')
axes[1].hist(c1, 11, density=True)
axes[1].hist(c2, 11, density=True)
axes[1].set_title('Recreate a BitGenerator')
plt.show()
