Pseudorandomization IndexError: list index out of range

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10): Win10
PsychoPy version (e.g. 1.84.x): 2022.2.3
Standard Standalone? (y/n) If not then what?: y
What are you trying to achieve?: Pseudorandomize trials so that no more than 2 consecutive trials of the same type occur

What did you try to make it work?: Used custom function adapted from Pseudo-randomization of stimuli trials in a loop - #3 by miguel_santin

**What specifically went wrong when you tried that?:**IndexError: list index out of range

Hi everyone,

I found all previous discussions on the topic of pseudorandomization and still didn’t find an answer. I have twelve lists with conditions that need to be pseudorandomized on the fly so that no more than 2 consecutive trials of the same type appear. I cannot generate different lists and then play them in a sequential order because I already have twelve lists, so creating multiple versions of each of them is unfeasible. Now, I used this function to achieve my goal:

Begin Experiment

import random
def pseudoRand(excelFile):
    # Load trials
    conditionMet = False
    # Pseudo-randomize trials
    while conditionMet == False:
        conditions = []
        for row in range(len(trials)):
            conditions.append(trials[row]["sentence_cond"])  #Name of the conditions type column
        # Condition for pseudo-randomization: No more than 2 consecutive of the same type
        for row in range(len(trials) - 2):
            if trials[row]["sentence_cond"] == trials[row + 2]["sentence_cond"]:
                conditionMet = False
                conditionMet = True
    # Save the final order of the trials
    customOrder = []
    for row in range(len(trials)):
        customOrder.append(trials[row]["stim_order"]) #Name of the original index column
    return customOrder

And I have $pseudoRand(“ListA3.xlsx”) in my Selected Rows and ListA3.xlsx in Conditions.
Every time I try to run the experiment it crashes with this error:

Traceback (most recent call last):
2.9702     WARNING     C:\Users\User\AppData\Roaming\psychopy3\fonts\Helvetica.truetype doesn't have valid font family name
3.0705     WARNING     C:\Users\User\AppData\Roaming\psychopy3\fonts\Helvetica.truetype doesn't have valid font family name
0.8728     WARNING     Monitor specification not found. Creating a temporary one...
  File "G:\My Drive\World knowledge\PsychoPy experiment\WK_LAB -", line 1352, in <module>
    trialList=data.importConditions('ListA3.xlsx', selection=pseudoRand("ListA3.xlsx")),
  File "C:\Program Files\PsychoPy\lib\site-packages\psychopy\data\", line 463, in importConditions
IndexError: list index out of range
################ Experiment ended with exit code 1 [pid:27640] #################

Does somebody know what could possibly be causing this? I tried disabling different components but nothing works, the problem is clearly with the function.


well, I suspect the row

for row in range(len(trials) - 2):

This line of code will not work for the first (0) and second (1) row.

So, you need some code to deal with the first two rows. Simply start with the third (2) row 2.

BTW, if you plan to run the experiment online, do not import libraries. Rather use Builder to “program” your experiment and add the code via a code-component.

Best wishes Jens

1 Like

Thank you, Jens! I actually solved the problem by first saving the new pseudorandomized list to an excel file at the beginning of the experiment (I didn’t change the core code at all) and then using that excel file later in my trials. I still don’t know why it doesn’t work without saving the new order to excel. Thankfully, I am running it locally, so no Pavlovia for me!