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
    trials=data.importConditions(excelFile)
    conditionMet = False
    # Pseudo-randomize trials
    while conditionMet == False:
        random.shuffle(trials)
        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
                break
            else:
                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):
48
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 - Copy_lastrun.py", line 1352, in <module>
    trialList=data.importConditions('ListA3.xlsx', selection=pseudoRand("ListA3.xlsx")),
  File "C:\Program Files\PsychoPy\lib\site-packages\psychopy\data\utils.py", line 463, in importConditions
    trialList.append(allConds[int(ii)])
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.

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!

Hello,

I deleted my reply because it was simply wrong.

Best wishes Jens

Hi there, I have the exact same problem as you described here. But I don’t really understand your solution. Can you explain in more detail how to save the new pseudorandomized list to an excel file, and how to call this newly created excel file later during the trial routine? I know it’s been a long time, so this is a long shot. Thank you!