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.