I have a question about random of numpy, especially shuffle and seed.
'seed' is used for generating a same random sequence.
'shuffle' is used for shuffling something.
To shuffle two lists in the same order, this code works :
idx = [1, 2, 3, 4, 5, 6]
idx2 = [1, 2, 3, 4, 5, 6]
seed = np.random.randint(0, 100000)
np.random.seed(seed)
np.random.shuffle(idx)
np.random.seed(seed)
np.random.shuffle(idx2)
results :
[1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6]
[5, 3, 1, 2, 4, 6] [5, 3, 1, 2, 4, 6]
[1, 5, 3, 2, 4, 6] [1, 5, 3, 2, 4, 6]
[2, 5, 3, 4, 6, 1] [2, 5, 3, 4, 6, 1]
[2, 5, 6, 3, 4, 1] [2, 5, 6, 3, 4, 1]
[4, 5, 6, 1, 2, 3] [4, 5, 6, 1, 2, 3]
I can check that this code works well.
... omitted
Solved but, the question was not clear.
Redefine the problem in simplified version:
idx = [1, 2, 3, 4, 5, 6]
for i in range(10):
seed = np.random.randint(0, 10000)
idx2 = [1, 2, 3, 4, 5, 6]
np.random.seed(seed)
np.random.shuffle(idx)
np.random.seed(seed)
np.random.shuffle(idx2)
Then, for each iteration, idx != idx2 is clear.
- The question is about this : Why are idx and idx2 not same?
But, I was not noticed re-initialization of idx2. ( Actually, the original code is not simple as this - for each iteration, idx2 gets new directories of images. - "imlist" in the answer plays the same role of idx2 in simplified version.)
After reading @tel 's comments, I found the problem. - idx should be also reinitialized or just use index based shuffling.
Fixed Version
for i in range(10):
seed = np.random.randint(0, 10000)
idx2 = [1, 2, 3, 4, 5, 6]
idx = [1, 2, 3, 4, 5, 6]
np.random.seed(seed)
np.random.shuffle(idx)
np.random.seed(seed)
np.random.shuffle(idx2)
Then, idx == idx2 : True