Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set numpy.random.seed on a global scale [closed]

How can I set numpy.random.seed(0) on a global scale? It seems I have to reset the seed every time I call a np.random function.

import numpy as np

np.random.seed(0)
print(np.random.randint(0,10,2))
np.random.seed(0)
print(np.random.randint(0,10,2))
print(np.random.randint(0,10,2))

np.random.seed(0)
print(np.random.rand())
np.random.seed(0)
print(np.random.rand())
print(np.random.rand())


[5 0]
[5 0]
[3 3]
0.5488135039273248
0.5488135039273248
0.7151893663724195
like image 429
Andi Avatar asked Dec 05 '25 18:12

Andi


1 Answers

That is how seeds actually work. You set a 'seed' value which determines all following generated random numbers. You can think of the seed as a starting point for randomly generated numbers. Every time you set the seed you set a starting point for generating a random sequence.

Every time the code generates a random number, it steps 'forward' from the seed/starting point in a random (but deterministic) way. Setting the seed puts the random number generator in a specific state from which it will follow the same random path every time (due to the is the deterministic character).

like image 197
Andre Avatar answered Dec 08 '25 08:12

Andre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!