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.
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:
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?
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.
You are right. Sorry just tested the “PsychoJS”-Part.
Two steps to make it work:
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).
Hello! I am trying this method and it works fine locally, however when I try to run it online I get the following error: “Uncaught TypeError: Failed to resolve module specifier “math”. Relative references must start with either “/”, “./”, or “…/”.”
Okay I figured out my issue. To run locally, you need to “import floor from math” in python, but if you do this in js, js doesn’t know what you want. I needed to include this code ONLY for python, and ensure that it was not being translated to js and included in my online js script. I did this by selecting “both” (or “py”) at the top of my code component, which is what @Luke described earlier.
Hi! I came across this thread while troubleshooting something in a task of mine and think this might fix my issue, however, is there a way to get it so that each row is only used once (i.e., randomly choose one row without replacement)? For context, I’m using conditional branching to make an approach-avoidance task where participants choose between two stimuli and within each stimulus that they could choose there are 20 possible outcomes creating different ratios of threat and reward for each stimulus, so each row can only be used once to keep the ratios correct.
Your error message refers to “Before JS Experiment”. What have you got in your Before Experiment tab? Did you put the code that should be in a Python only code component into an Auto code component?
I think that maxrows is about the number of conditions, not the number of rows in each condition.
Another way of randomly selecting one row is to end the outer loop after one iteration (using outer_loop_name.finished = True)
If you want the number of participants in each condition to be the same then I would recommend a counterbalance routine (locally – there are still a few bugs in the online version).