Press "Enter" to skip to content

Another Python script for randomization with constraints

Here you will find a Python function for randomization with constrains. This was written, specifically, for a psychological experiment (a shifting/task-switching task) I was planning to conduct. For this experiment, I needed a list of stimuli names that were quasi-randomized.  Fifty percent of the items in the list are followed by a stimulus in the same colour and the remaining 50 % are followed by a stimulus in a different colour.

Python script

The last code snippet, below, crucial for the script to be fully functional since. That is, sometimes the randomization will fail and, therefore, the code below first checks that the wanted output has been generated. That is, it is 50% items that are of a color and then followed by a different color? An additional check is also made to make sure that there are as many items that were  required (i.e-, number of trials; ntrials). The last line of code call the function, with a list, and number of trials.

The script solved my problem of quazi-randomization and it is pretty quick. However, if you know a better method I would love to see it.

Link to the full script on pastebin. If you are interesting in another randomization script using PsychoPy click here.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: