Randomising stimuli for each participant without breaking constraints

OS (e.g. Win11):
PsychoPy version (e.g. 2025.1.1)

I need my stimuli to randomise for each participant on Pavlovia, but when I use shuffle or try to call my randomised trials file it breaks my other constraints (max 3 consecutive male/female audio and max 3 consecutive conditions). How can I randomise my stimuli without uploading hundreds of separate experiments?

Many thanks in advance.

Have a look at my Complex Randomisation and Randomisation without repetition online demos. Trial switching might help too.

Hi, thank you for your response. I’m afraid I’m very new to this and your reply doesn’t make much sense to me.


I’m running a memory experiment on Pavlovia and need help implementing per-participant randomization while maintaining trial order constraints.

My experiment:

  • 30 encoding trials per list (2 lists total)

  • Each trial has constraints: max 3 consecutive of same condition type (A/B) and max 3 consecutive of same voice (male/female)

  • I need each participant to get a unique randomized order that respects these constraints

What I’ve tried:

I’ve pre-generated 5 valid trial orders (trials_list1_v1.xlsx through v5.xlsx, same for list 2) that all satisfy the constraints. My goal was to have PsychoPy randomly select one of these files for each participant.

In a code component (Before Experiment), I tried:

python

import random
import pandas as pd

selected_order = random.randint(1, 5)
trials_list1 = pd.read_excel(f'trials_list1_v{selected_order}.xlsx')
trials_list2 = pd.read_excel(f'trials_list2_v{selected_order}.xlsx')

Then set my loop Conditions fields to the variables trials_list1 and trials_list2.

The problem:

  • This works fine locally in Builder

  • On Pavlovia, the experiment hangs at initialization

  • I believe the issue is that the JavaScript equivalent doesn’t work (no easy way to load Excel files in JS before the loops initialize)

  • I also tried using variables in the Conditions field (e.g., $list1_file) but PsychoPy doesn’t allow this

My question: Is there a way to have Pavlovia dynamically select which trial file to use for each participant? It seems like such a massive flaw to not be able to randomise stimuli with constraints.

My university tech support suggested this should be possible by “randomly selecting a file at the start of the experiment” but I can’t figure out how to implement it in a way that works on Pavlovia.

Any guidance would be greatly appreciated!

Environment:

  • PsychoPy version: 2025.1.1

  • Running on: Pavlovia

  • Browser: Chrome

You are making life difficult for yourself by using code for standard PsychoPy Builder components.

Try this code.

selected_order = randint(5) + 1 # I think randint generates a number from 0 to n-1
trials_list1 = 'trials_list1_v' + str(selected_order + '.xlsx'
trials_list2 = 'trials_list2_v' + str(selected_order + '.xlsx'

Then put $trials_list1 and $trials_list2 in your loop definitions. Don’t worry about the warning that it can’t find the spreadsheet, though you could select one of your orders first to check before switching to the variable.

I was assuming that you wanted to generate random trial lists on the fly with complex randomisation, rather than randomly select from a set of pre-randomised orders. Make sure your loops are set to sequential or you will not get the order you selected.

Thank you for your reply. Your suggestion pointed me in the right direction.

I ended up implementing the on-the-fly randomization in JavaScript rather than selecting from pre-made files. I converted my Python code to JavaScript in the “Before JS Experiment” tab and added code components in each trial routine to extract variables from the current trial dictionary. Seems to be working!

Thanks again for the guidance - your insight about not needing to pre-generate files was spot on!