Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Cheers!

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading