| Reference | Downloads | Github

Do we need to seed the random generator?


I noticed that random.seed() is not one of the functions imported by psychopy. Is it recommended to use np.random.seed(), or is the random number generator seeded automatically?


You can import functions yourself if needed, but the first question is why you want to set the seed? Seeding a random number generator is for when you don’t want randomness. Or more strictly, when you want a sequence of pseudo-random numbers that will be repeated every time you run the experiment, because the algorithm will always begin from the same starting value.

Is that what you need? If so, you can set a seed for a loop, for example, by specifying a seed value in the loop dialog. There are valid reasons for doing that, which will be explicitly specified in your experimental design.

Lastly, you’ve indicated that the question is about online experiments. numpy is a Python library, which isn’t available online. So you’d need to use a JavaScript alternative for any custom code (setting the seed in a loop dialog should still work, I guess).

Based on @Michael ‘s reply your answer is no.

I seem to remember that in MATLAB the default behaviour was to generate the same pseudo random numbers every time, which caused me a problem for some of my early experiments until I started adding a seed based on time. You don’t need to do this in PsychoPy (offline or online)

Yes indeed, I guess I didn’t answer the question directly. To clarify, whether you’re using the Python standard library or numpy, when you import the library, it will start with a seed based on the current time and possibly other sources of entropy, which depend on your operating system. This will automatically result in a fresh random sequence every time your code runs.

You only need to set the seed manually if you want a replicable “random” sequence.

@wakecarter thank you! I come from a MATLAB background so I was used to manually setting the seed with clock at the beginning of each of my experiments. Good to know python does this all by itself like a grownup. :wink:

1 Like