In a python console, you have to set the seed at each run if you want to keep the output the same.
In[0]: import numpy as np
In[1]: np.random.seed(1); np.random.randint(1, 10, 5)
Out[0]: array([6, 9, 6, 1, 1])
In[2]: np.random.seed(1); np.random.randint(1, 10, 5)
Out[1]: array([6, 9, 6, 1, 1])
In[3]: np.random.randint(1, 10, 5)
Out[2]: array([2, 8, 7, 3, 5]) # Different output if the seed was not set
However, when it comes to running code with multiple files, the random functions in one file would be affected by the seed set in another imported module, which may cause some unexpected issues.
Say, I have two files
# main.py
from myfunc import *
import numpy as np
myfunc()
print('main.py:', np.random.randint(1, 10, 5))
and
# myfunc.py
import numpy as np
def myfunc():
np.random.seed(2019)
numbers = np.random.randint(1, 10, 5)
print('myfunc:', numbers)
If I run main
twice, I'll get the same results
myfunc.py: [9 3 6 9 7]
main.py: [9 1 1 8 9]
and
myfunc.py: [9 3 6 9 7]
main.py: [9 1 1 8 9]
This implies that the randint
was seeded even it was not set in the main.py
. Seeing this, I guess that was because np.random.seed()
works somewhat globally. And I should use it carefully, particularly when I just want it to work locally.
My solution so far is reset the seed whenever I finish using it. Like
np.random.seed(2019)
numbers = np.random.randint(1, 10, 5)
np.random.seed()
I am not sure what the working range of `np.random.seed()`` is. And is there any other way to avoid the global setting issues?
Aucun commentaire:
Enregistrer un commentaire