How can I randomly choose one row in the condition file?

PsychoPy 2021.2.3 on mac
What are you trying to achieve?: I want it to randomly choose one of the 4 trials


HEXvOi2Gxpzz5mM6XJmj3cCmN.jpeg)

What did you try to make it work?: I tried to create a bigger loop with this condition file; however, I want it to randomly choose only one row, but I didn’t know how to do it.

What specifically went wrong when you tried that?: It ran every row in random, or if I put a number of rows in the selected row, it only runs that particular row. so I would like to know how can I make it random.

You can select a single random row in the “Loop-Selection”-File with a variable in “Selected Rows”

Just use a Code-Component to generate a random number between 0 and (maxRows) of the “Selection-Loop”-File:

Notice: I substract 0.001 from maxRows to avoid return of 4 in Math.floor if random() outputs exactly 1.

Python Code to Copy in Auto-Translate Code Component (Begin Experiment).

#Rows of the Loop Selection Sheet
maxRows = 4 
#Substract a small number to avoid floor(maxRows)==maxRows
maxRows = maxRows-0.0001
# Returns values only between 0 and maxRows 
sheetRowIndex = str(floor(random()*(maxRows)))
print(sheetRowIndex)

To ensure it is working in the Python-Run (not just online) as well:

Add another Py-Code-Component like this:

Best, Luke

Thank you so much for your help!
However, I would like to ask more about the variables in selected rows. What should I write in my excel file for that? Do I make a new column?

No need to change anything in your Excel-file.

I thought you used the “counterbalance”-Loop with the screenshot-presented “loopselectone.xlsx”.
As you already implemented the nRepsA column etc. looks fine to “choose” between the four different trials.

Now you can just add an code-component before the counterbalance-Loop as described to generate the random-selection-number for the loopselectone.xlsx in the counterbalance-Loop.

So basically everything you did looks fine and just add the $sheetRowIndex to the “Selected Rows”-Field of the counterbalance-Loop. In my code $sheetRowIndex just is a random integer between 0 and 3. So in your example for the loopselectone.xlsx, if $sheetRowIndex is e.g. 2, row 3 of your file will be excecuted (nRepsC = 1) (Note: Indexing number 2 leads to row 3 because indexing starts here with 0) - leading to show trial3-Loop in your example.

image

So the counterbalance loop should look like this.

When I run it, it said
sheetRowIndex = floor(random()*(maxRows))
NameError: name ‘floor’ is not defined
How can I fix it? thank you.

You are right. Sorry just tested the “PsychoJS”-Part.
Two steps to make it work:

  1. Update the Code above to this (I will update the original answer).
#Rows of the Loop Selection Sheet
maxRows = 4 
#Substract a small number to avoid floor(maxRows)==maxRows
maxRows = maxRows-0.0001
# Returns values only between 0 and maxRows 
sheetRowIndex = str(floor(random()*(maxRows)))
print(sheetRowIndex)

2.just add another code component to make “floor” available in the Python-Experiment. (Python only/Before Experiment).

Now everything should work fine (offline and online).

I got it now. Thank you so much! :smiley: