# Randomise code in a loop

**URL:** <https://discourse.psychopy.org/t/randomise-code-in-a-loop/16586>\
**Category:** Builder\
**Created:** [September 18, 2020, 9:56am UTC](https://discourse.psychopy.org/t/randomise-code-in-a-loop/16586 "2020-09-18T09:56:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mothos](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/mothos/32/3634_2.png) [@mothos](https://discourse.psychopy.org/u/mothos)\
**Post date:** [September 18, 2020, 9:56am UTC](https://discourse.psychopy.org/t/randomise-code-in-a-loop/16586/1 "2020-09-18T09:56:23Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![mothos](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/mothos/32/3634_2.png) [@mothos](https://discourse.psychopy.org/u/mothos)\
**Post date:** [September 21, 2020, 11:41am UTC](https://discourse.psychopy.org/t/randomise-code-in-a-loop/16586/2 "2020-09-21T11:41:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TParsons](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/tparsons/32/8301_2.png) [@TParsons](https://discourse.psychopy.org/u/TParsons)\
**Post date:** [September 22, 2020, 8:24am UTC](https://discourse.psychopy.org/t/randomise-code-in-a-loop/16586/3 "2020-09-22T08:24:24Z")

</div>

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

````auto
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:

```auto
$commandStart[commandStartKey]

```

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