Randomise code in a loop

Hi,

This experiment I intend to run is connected to a custom-built MR safe device delivering mechanical pain.
In the current project I want to be able to randomise between 5 different levels of pain it can administer but I’m not sure how to do that in the builder. I also want to make sure that they are all run the same amount of times and that I can track the order they were run in as the participant is supposed to rate each instance.

The following 5 lines are what I want to have randomised in the loop. They should all be run 10 times each.

commandStartTarget = bytearray(['S', commandStartCode, target, speed, holdtime, phbit])
commandStartTargetplus5 = bytearray(['S', commandStartCode, targetplus5, speed, holdtime, phbit])
commandStartTargetminus5 = bytearray(['S', commandStartCode, targetminus5, speed, holdtime, phbit])
commandStartTargetplus10 = bytearray(['S', commandStartCode, targetplus10, speed, holdtime, phbit]),
commandStartTargetminus10 = bytearray(['S', commandStartCode, targetminus10, speed, holdtime, phbit])

Thank you.

Anyone able to help? I’ve been trying but really can’t wrap my head around it.

Rather than storing them as variables, you could store them as keys in a dict, like this:

commandStart = {
    "zero": bytearray(['S', commandStartCode, target, speed, holdtime, phbit]),
    "plus5": bytearray(['S', commandStartCode, targetplus5, speed, holdtime, phbit]),
    ```etc.```
}

Then you can create a column in a conditions file called something like commandStartKey with all of the keys in this dict, putting it in a loop with loopType set to random, so it will choose keys at random but in balanced proportions.

Then you can choose a value from commandStart and store it in a Variable component by setting the Routine Start Value of that Variable to:

$commandStart[ commandStartKey ]

and make sure that “save routine start value” is ticked, then you will have the variable value in your datafile.

1 Like