[FieldTrip] Unwanted behavior of matlabs randomization functions?

maurice.goeldi at uzh.ch maurice.goeldi at uzh.ch
Wed May 10 11:24:30 CEST 2017

Dear Fieldtrippers

I have just become aware of a 'feature' in matlabs randomization functions (rand, randi, randn and randperm).
Every time matlab is restarted, the seed for the random number generator is reset.
Therefore these functions always produce the same number sequence after a restart.
This seems a very unintuitive use of these functions.
I would think that people who need this reproducability should manually set a seed for their random number generator.

Is this behaviour really wanted in FT?
I did a quick check and ran ft_freqstatistics with montecarlo method.
The stat.posdistribution is obviously different when i run it consecutive times. 
However when I restart matlab I get exactly the same distributions again each time.
I realize that in a standard analysis pipeline this is probably of absolutely no consequence. 
But I still think it should not be that way and one should at least be aware of the fact.

Even if it is not an issue for FT-analyses, this might very well be so for experimental procedures.
E.g. some experimental stimuli are randomized for each subject.
Then they are presented in randomized order.
By the time the next subject comes in, chances are high that the computer or at least matlab has been restarted. 
This subject will therefore recieve exactly the same 'randomized' list.
Mostly these stimuli randomizations are checked by running the randomization script multiple times. So everything seems to work.
Up to now at least I have never tested my scripts by restarting matlab after every run.
And to catch this within the experiment is potentially impossible, depending on the procedure.

Fortunately there is an easy fix for this.
before using the randomization function shuffles the seed for the random number generator (hopefully randomly).

Even if this is not an issue for FT directly, I think many people do not know about this suboptimal (in my opinion) behaviour of the randomization functions and run into trouble.

Appologies if this is an obvious and long known 'feature' to most of you.
I hope it still helps some people getting strange results.


University of Zürich
Maurice Göldi, MSc
Department of Psychology

maurice.goeldi at uzh.ch
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.science.ru.nl/pipermail/fieldtrip/attachments/20170510/24399d2e/attachment-0001.html>

More information about the fieldtrip mailing list