Strange numpy random shuffle and seed

Viewed 20188

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

1 Answers

So it looks like, as you said, the changes to imlist are the source of confusion. ix1 and ix2 continue to change in lockstep with one another, but the order of imlist is refreshed at the start of each loop. Since, for example, ix1 and imlist start out in a different order at the start of most loops (all except the first), of course shuffle will leave them in a in a different order, regardless of the random seed.

Related