Skip to content

numpy/legacy-random

Rule legacy-random
Pack NumPy
Severity warning
Fix none, edit by hand
Library versions every version

np.random.seed and the functions beside it use a hidden global state that NumPy keeps only for old code. Create rng = np.random.default_rng(seed) once and draw from rng.

np.random.seed(0)
idx = np.random.choice(len(x), 100, replace=False)
rng = np.random.default_rng(0)
idx = rng.choice(len(x), 100, replace=False)

The rule reports np.random.seed, np.random.rand, np.random.randint, np.random.randn, np.random.choice, every other distribution function in np.random, np.random.get_state, np.random.RandomState(...) outside files that import scikit-learn, and imports of the same names from numpy.random. The message names the Generator method, such as integers for randint. When the method ran at least 1.5 times faster, the message also gives the speedup in the table.

Generator method Faster by
random 2 times
integers 2.5 times
exponential 2.4 times
choice(..., replace=False) 280 times

xpd has no automatic fix. A Generator draws a different stream from the same seed, and np.random.seed elsewhere would no longer reach the rewritten call. Make the change by hand.

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

$NP.random.seed sets the hidden global RandomState, which NumPy keeps only for old code. Create rng = $NP.random.default_rng(seed) once and draw from rng.

Browse the other rules of the NumPy pack.