np.random.permutation with seed?
np.random.seed(42)
np.random.permutation(10)
If you want to call np.random.permutation(10)
multiple times and get identical results, you also need to call np.random.seed(42)
every time you call permutation()
.
For instance,
np.random.seed(42)
print(np.random.permutation(10))
print(np.random.permutation(10))
will produce different results:
[8 1 5 0 7 2 9 4 3 6]
[0 1 8 5 3 4 7 9 6 2]
while
np.random.seed(42)
print(np.random.permutation(10))
np.random.seed(42)
print(np.random.permutation(10))
will give the same output:
[8 1 5 0 7 2 9 4 3 6]
[8 1 5 0 7 2 9 4 3 6]
If you want it in one line, you can create a new RandomState
, and call the permutation
on that:
np.random.RandomState(seed=42).permutation(10)
This is better than just setting the seed of np.random
, as it will have only a localized effect.