I have a function and a subfunction and in each of them some random arrays are being generated. In order to make the result reproducible, I use seed. However I see strange cases.
When, I have a seed in the subfunction, the random numbers in the main function also get an effect from the seed in subfunction. And, there is not such effect from main function to subfunction. For example, consider the following code.
import random
from random import randint
import numpy as np
def randgene():
a=np.random.randint(0,5,size=5)
print "a in function", a
np.random.seed(seed=15)
b=np.random.randint(0,5,size=5)
print "b in function", b
d=np.random.choice(50, size=5, replace = False)
print "d in function", d
# np.random.seed(seed=None)
def main():
print "d-without seed", np.random.choice(50, size=5, replace = False)
print "a-without seed", np.random.randint(0,5,size=5)
print "b-without seed", np.random.randint(0,5,size=5)
f = randgene()
print "d-without seed", np.random.choice(50, size=5, replace = False)
print "a-without seed", np.random.randint(0,5,size=5)
print "b-without seed", np.random.randint(0,5,size=5)
f = randgene()
print "d-without seed", np.random.choice(50, size=5, replace = False)
print "a-without seed", np.random.randint(0,5,size=5)
print "b-without seed", np.random.randint(0,5,size=5)
np.random.seed(seed=10)
print "d-with seed", np.random.choice(50, size=5, replace = False)
print "a-with seed", np.random.randint(0,5,size=5)
print "b-with seed", np.random.randint(0,5,size=5)
f = randgene()
print "d-with seed", np.random.choice(50, size=5, replace = False)
print "a-with seed", np.random.randint(0,5,size=5)
f = randgene()
print "d-with seed", np.random.choice(50, size=5, replace = False)
print "a-with seed", np.random.randint(0,5,size=5)
if __name__ == '__main__':
main()
For this code I get this result:
d-without seed [14 29 9 42 18]
a-without seed [3 0 0 3 4]
b-without seed [3 2 0 2 1]
a in function [2 3 1 2 3]
b in function [0 4 0 4 3]
d in function [41 16 22 24 14]
d-without seed [ 8 21 32 39 11]
a-without seed [3 0 3 3 0]
b-without seed [1 2 2 1 4]
a in function [4 4 0 2 2]
b in function [0 4 0 4 3]
d in function [41 16 22 24 14]
d-without seed [ 8 21 32 39 11]
a-without seed [3 0 3 3 0]
b-without seed [1 2 2 1 4]
d-with seed [37 23 44 42 47]
a-with seed [2 0 0 4 4]
b-with seed [0 0 2 4 2]
a in function [0 0 2 3 0]
b in function [0 4 0 4 3]
d in function [41 16 22 24 14]
d-with seed [ 8 21 32 39 11]
a-with seed [3 0 3 3 0]
a in function [1 2 2 1 4]
b in function [0 4 0 4 3]
d in function [41 16 22 24 14]
d-with seed [ 8 21 32 39 11]
a-with seed [3 0 3 3 0]
in which you see d-with seed [ 8 21 32 39 11], a-with seed [3 0 3 3 0]
repeated in main function, whenever I have called the subfunction.
However, if I comment the line np.random.seed(seed=15)
in the subfunction I get this result:
d-without seed [17 20 23 36 28]
a-without seed [3 1 1 2 0]
b-without seed [3 2 1 1 3]
a in function [1 2 2 0 4]
b in function [4 4 0 4 2]
d in function [ 9 46 19 7 47]
d-without seed [39 42 10 17 4]
a-without seed [2 3 0 2 4]
b-without seed [1 4 1 3 2]
a in function [1 1 3 3 2]
b in function [1 3 4 4 3]
d in function [ 0 2 45 5 19]
d-without seed [24 20 47 3 29]
a-without seed [3 0 3 3 3]
b-without seed [1 0 0 2 3]
d-with seed [37 23 44 42 47]
a-with seed [2 0 0 4 4]
b-with seed [0 0 2 4 2]
a in function [0 0 2 3 0]
b in function [4 4 0 1 1]
d in function [ 6 11 35 4 7]
d-with seed [19 47 43 38 15]
a-with seed [0 4 2 1 2]
a in function [1 2 1 3 2]
b in function [3 4 4 0 2]
d in function [38 31 17 43 2]
d-with seed [ 7 15 39 2 49]
a-with seed [3 4 1 4 0]
which apparently has no repeating. So, the seed in the main function has no effect in the subfunction.
If I keep both np.random.seed(seed=15)
and np.random.seed(seed=None)
uncommented, I will get similar result.
May someone explain me what is happening?
Thanks in Advance, Afshin