Home > other >  np.random.shuffle() with seed giving different shuffles every time
np.random.shuffle() with seed giving different shuffles every time

Time:01-22

I have a lengthy list that stores ~127k integers. I want to shuffle the list with a seed for reproducible results later on. However, it seems as though every time I execute the code, the list gets re-shuffled. This is the code:

import numpy as np
np.random.seed(5)
np.random.shuffle(labels)
print(labels[:5])

The print statement at the end gives different results on each run, e.g.:

[124802, 178520, 155245, 127235, 146541]
[31595, 120382, 47991, 141687, 62795]
[13991, 67563, 69090, 76243, 184231]

I apologize if I'm overseeing something very obvious. Hopefully someone can take a few mins out of their time to help!

Cheers!

CodePudding user response:

Recreate a labels variable and the result will be the same every time. Here you are just creating labels once and executing the shuffling code several time.

np.random.shuffle modifies your list in-place. That also explains why a return value is not necessary.

  •  Tags:  
  • Related