# Blocks not randomized in pavlovia

**URL:** <https://discourse.psychopy.org/t/blocks-not-randomized-in-pavlovia/26677>\
**Category:** Other\
**Tags:** randomization, blocking, pavlovia\
**Created:** [January 4, 2022, 7:28pm UTC](https://discourse.psychopy.org/t/blocks-not-randomized-in-pavlovia/26677 "2022-01-04T19:28:20Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![B.D.5](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@B.D.5](https://discourse.psychopy.org/u/B.D.5)\
**Post date:** [January 4, 2022, 7:28pm UTC](https://discourse.psychopy.org/t/blocks-not-randomized-in-pavlovia/26677/1 "2022-01-04T19:28:20Z")

</div>

Hello,

My first Q is, to randomize the 2 routines under ret1\_rand and ret2\_rand. Although everything was set the same way, randomization under the ret2\_rand loop was successful but **ret\_1rand repeats each routine twice** , why?  
code in **Each frame** (_also tried in begin routine_):

```auto
if nRepsImlist == 0: #in the first routine
    continueRoutine = False

if nRepsWlist == 0: #in the second routine
    continueRoutine = False

#the same is repeated for ret2_rand
if nRepsImlist2 == 0: #in the first routine
    continueRoutine = False

if nRepsWlist2 == 0: #in the second routine
    continueRoutine = False

```

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/8/f/8fbb1ba3019ed607479ede67e7527fb251a5f27a.png)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/1/0/105204e104aa8de9355838c66b4c2fb732832a37.png)

The second Q is I wanted to randomize 2 blocks in the same way. I have 2 blocks in my study (see image). I added these blocks as loops but it did not help and I removed them and demonstrated them in the image.

 ![blocks](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/c/0/c00d0117b39ea0370e168a1899917161d12a4337.jpeg)

In the first routine of **B1** , each frame:

```auto
if nRepsIm == 0:
    continueRoutine = False

```

In the first routine of **B2** , each frame:

```auto
if nRepsLan == 0:
    continueRoutine = False

```

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/5/8/58e294371d70b7768795b61ac11760cf1541942b.png)  
**I tried this by adding a loop around the each block and setting nReps as nRepsIm and nRepsLan but it did not work either. It finishes the first block, then starts the second (not successful randomization) and after the second block it repeats the first block.**

I spent a few days creating random blocks of trials (successfully) however later it did not work online (a problem in the data output). I need to start collecting data soon. Your urgent help is much appreciated.

---

<div class="post-metadata">

**Author:** ![B.D.5](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@B.D.5](https://discourse.psychopy.org/u/B.D.5)\
**Post date:** [January 5, 2022, 7:15am UTC](https://discourse.psychopy.org/t/blocks-not-randomized-in-pavlovia/26677/2 "2022-01-05T07:15:43Z")

</div>

- to randomize the blocks, I added

```auto
if nRepsLan == 0:
    continueRoutine = False

```

this code in every routine – each frame under Block2

and

```auto
if nRepsIm == 0:
    continueRoutine = False

```

under the every routine – each frame of Block1.  
did not work either

---

<div class="post-metadata">

**Author:** ![mmagnuski](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/mmagnuski/32/11741_2.png) [@mmagnuski](https://discourse.psychopy.org/u/mmagnuski)\
**Post date:** [January 10, 2022, 11:51am UTC](https://discourse.psychopy.org/t/blocks-not-randomized-in-pavlovia/26677/3 "2022-01-10T11:51:34Z")

</div>

Hi @B.D.5,  
Let’s start with your first question. Could you please clarify what you are trying to achieve? You write:

> [@B.D.5](#):
>
> My first Q is, to randomize the 2 routines under ret1\_rand and ret2\_rand. Although everything was set the same way, randomization under the ret2\_rand loop was successful but **ret\_1rand repeats each routine twice** , why?

Do you mean that your two loops are both set to loopType “random” but only one has random trial order with respect to the excel file? If so - does it happen only on Pavlovia or also locally? Last question - what is the code that you pasted trying to achieve?

---

<div class="post-metadata">

**Author:** ![mmagnuski](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/mmagnuski/32/11741_2.png) [@mmagnuski](https://discourse.psychopy.org/u/mmagnuski)\
**Post date:** [January 10, 2022, 12:01pm UTC](https://discourse.psychopy.org/t/blocks-not-randomized-in-pavlovia/26677/4 "2022-01-10T12:01:27Z")

</div>

Now your second question:

> [@B.D.5](#):
>
> The second Q is I wanted to randomize 2 blocks in the same way.

do you mean that you want to have random order of the blocks? So that sometimes block B2 is first and sometimes B1? There are two relatively easy ways to achieve this:

1. have three blocks: B1, B2, B1, each in a loop where nReps for both B1 loops is controlled by two different variables: `n_b1_first`, `n_b1_second` for example. If you want B2, B1 order you set `n_b1_first` to zero and `n_b1_second` to `1`. If you want B1, B2 then `n_b1_first = 1` and `n_b1_second = 0`.
2. If your B1 and B2 are identical in structure (as your screenshot suggests) - then maybe you just need one block and a few variables to control whether this block behaves as B1 or B2.

---

<div class="post-metadata">

**Author:** ![B.D.5](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@B.D.5](https://discourse.psychopy.org/u/B.D.5)\
**Post date:** [January 12, 2022, 12:25pm UTC](https://discourse.psychopy.org/t/blocks-not-randomized-in-pavlovia/26677/5 "2022-01-12T12:25:13Z")

</div>

Thank you so much for the answer, as you see in the images, they all are set to random but loop in the first block is repeating itself 2 times but not in the second block.

The code is reading the excel file in the outer loop. If the relevant nReps is 0,it passes the that routine and starts with the second, if 1, it initiates the loop.

---

<div class="post-metadata">

**Author:** ![B.D.5](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@B.D.5](https://discourse.psychopy.org/u/B.D.5)\
**Post date:** [January 12, 2022, 12:30pm UTC](https://discourse.psychopy.org/t/blocks-not-randomized-in-pavlovia/26677/6 "2022-01-12T12:30:59Z")

</div>

> [@mmagnuski](#):
>
> If your B1 and B2 are identical in structure (as your screenshot suggests) - then maybe you just need one block and a few variables to control whether this block behaves as B1 or B2.

I understood your point, In my comment below, I tried to do it but I guess in the wrong way. Do you have any suggestions?

> [@B.D.5](#):
>
> - to randomize the blocks, I added
> 
> ```auto
> if nRepsLan == 0:
> continueRoutine = False
> 
> ```
> 
> this code in every routine – each frame under Block2
> 
> and
> 
> ```auto
> if nRepsIm == 0:
> continueRoutine = False
> 
> ```
> 
> under the every routine – each frame of Block1.  
> did not work either

---

<div class="post-metadata">

**Author:** ![mmagnuski](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/mmagnuski/32/11741_2.png) [@mmagnuski](https://discourse.psychopy.org/u/mmagnuski)\
**Post date:** [January 12, 2022, 1:07pm UTC](https://discourse.psychopy.org/t/blocks-not-randomized-in-pavlovia/26677/7 "2022-01-12T13:07:44Z")

</div>

Just to make sure we are on the same page:

- loop is used to repeat a routine or a sequence of routines (with different variables for example), not to randomize the order of these routines
- `loopType` set to “random” does not specify number of repetitions, only how rows are drawn from the excel file
- the `nReps` for both loops (`ret1_rand` and `ret2_rand`) in your screenshot is `1` so the excel file with the conditions should repeat once in each loop. However, the loops use different excel files, so their length may differ.

> I understood your point, In my comment below, I tried to do it but I guess in the wrong way. Do you have any suggestions?

I’m still not sure what you are trying to achieve. Do you want to randomize the **order of blocks** or the **order of trials** within each block?  
If you want to randomize the order of blocks, then I’ve already suggested an outline of a possible solution.

---

<div class="post-metadata">

**Author:** ![mmagnuski](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/mmagnuski/32/11741_2.png) [@mmagnuski](https://discourse.psychopy.org/u/mmagnuski)\
**Post date:** [January 12, 2022, 1:10pm UTC](https://discourse.psychopy.org/t/blocks-not-randomized-in-pavlovia/26677/8 "2022-01-12T13:10:34Z")

</div>

BTW, you don’t need the code that you paste in each frame of the loop - just set `nReps` in the loop gui to the relevant variable (for example `nRepsLan`).

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [January 12, 2022, 1:19pm UTC](https://discourse.psychopy.org/t/blocks-not-randomized-in-pavlovia/26677/9 "2022-01-12T13:19:20Z")

</div>

The reason @B.D.5 is using that code is to remove excess blank lines in the data file that you get from the method of variable nReps.

> [@Block order randomization](https://discourse.psychopy.org/t/block-order-randomization/26588/16):
>
> As previously mentioned, the istrials flag doesn’t do anything online. You can reduce the number of rows online by using different techniques, such as: if nReptsImlist == 0: continueRoutine = False in Begin Routine of imret2\_loop instead of having a loop around it with nReps of nReptsImlist

---

<div class="post-metadata">

**Author:** ![B.D.5](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@B.D.5](https://discourse.psychopy.org/u/B.D.5)\
**Post date:** [January 12, 2022, 1:28pm UTC](https://discourse.psychopy.org/t/blocks-not-randomized-in-pavlovia/26677/10 "2022-01-12T13:28:57Z")

</div>

For my second Q:  
I want to randomize the order of Blocks as indicated in the image. Sometimes B1 will come, sometimes B2. However, creating several inner and outer loops ended up problematic [data output](https://discourse.psychopy.org/t/block-order-randomization/26588/7). That is why I wanted to randomize the blocks with a code. So, ret1 and ret2 do not matter they are just for randomizing the order of routines.

In this suggestion, I did not understand how you defined n\_b1\_first cause in my Control loop, the nReps are defined as nRepsIm (for block1) and nREpsLan (for block2)

> [@mmagnuski](#):
>
> have three blocks: B1, B2, B1, each in a loop where nReps for both B1 loops is controlled by two different variables: `n_b1_first` , `n_b1_second` for example. If you want B2, B1 order you set `n_b1_first` to zero and `n_b1_second` to `1` . If you want B1, B2 then `n_b1_first = 1` and `n_b1_second = 0` .

---

<div class="post-metadata">

**Author:** ![mmagnuski](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/mmagnuski/32/11741_2.png) [@mmagnuski](https://discourse.psychopy.org/u/mmagnuski)\
**Post date:** [January 14, 2022, 12:56am UTC](https://discourse.psychopy.org/t/blocks-not-randomized-in-pavlovia/26677/11 "2022-01-14T00:56:12Z")

</div>

Thanks for clarifying, I noticed that pavlovia does not support “is trials” loop option, but I didn’t know this is a workaround for that.

---

<div class="post-metadata">

**Author:** ![mmagnuski](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/mmagnuski/32/11741_2.png) [@mmagnuski](https://discourse.psychopy.org/u/mmagnuski)\
**Post date:** [January 14, 2022, 1:18am UTC](https://discourse.psychopy.org/t/blocks-not-randomized-in-pavlovia/26677/12 "2022-01-14T01:18:21Z")

</div>

> [@B.D.5](#):
>
> I want to randomize the order of Blocks as indicated in the image.

Ok, thanks for clarifying. I just have one more question to be 100% sure: do you want to randomize whether B1 **OR** B2 is shown, but not necessarily the order in which both B1 **AND** B2 are shown? I ask becasue you mention “Sometimes B1 will come, sometimes B2”, which suggests that you want to show only one of these blocks per participant.

If you want to randomize the order of block presentation, so either “first B1, then B2” or “first B2, then B1” then my suggestion was to have three blocks in following order: B1a, B2, B1b (where B1a and B1b are identical). Then if you want `B1, B2` order you set variable that controls nReps of `B1b` to 0 (and other nReps variables - for `B1a` and `B2` - to 1). This way B1b is not shown but B1a and B2 are, giving you `B1, B2` order. On the other hand, if you want `B2, B1` order you set the variable that controls nReps of `B1a` to 0 (and other blocks’ nReps to 1). You get `B2, B1b` order and because both `B1a` and `B1b` are identical, effectively the order you get is `B2, B1`.

There might be a more elegant solution to your problem if you B1 and B2 blocks are similar enough. For example if the only difference between B1 nad B2 is the conditions file, then you could just change the conditions file name via some variable. But this requires knowing what happens in the routines that consitute B1 block and B2 block.

---

<div class="post-metadata">

**Author:** ![B.D.5](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@B.D.5](https://discourse.psychopy.org/u/B.D.5)\
**Post date:** [January 14, 2022, 6:17pm UTC](https://discourse.psychopy.org/t/blocks-not-randomized-in-pavlovia/26677/13 "2022-01-14T18:17:54Z")

</div>

> [@mmagnuski](#):
>
> If

Both blocks will be presented but the order will be randomized. I am a Pscyhopy novice so I am not sure if I understood you clearly. Do you mean adding custom variable in the first routines of the blocks? I never tried adding variables. Could you please describe more?

B1 and B2 are very similar, I copied B1 and created B2 as a second block and change a few things (e.g. cond file, scale points of the slider).

---

<div class="post-metadata">

**Author:** ![mmagnuski](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/mmagnuski/32/11741_2.png) [@mmagnuski](https://discourse.psychopy.org/u/mmagnuski)\
**Post date:** [January 14, 2022, 6:46pm UTC](https://discourse.psychopy.org/t/blocks-not-randomized-in-pavlovia/26677/14 "2022-01-14T18:46:11Z")

</div>

> [@B.D.5](#):
>
> Do you mean adding custom variable in the first routines of the blocks?

In general - yes. But the specifics depend on on how you want to control the ordering of the blocks. If you want it to be chosen randomly, once for the whole experiment, then you could add a code component to the first routine you have (the code component can be fund under Custom components). Then in the code component you’d have to choose “js” in the CodeType (as far as I know automatic python-\>js translation does not handle generating random values well, but I may be wrong). In the textbox under “Begin Experiment” you can then write:

```auto
randval = Math.random()

if (randval < 0.5) {
  n_rep_b1a = 1;
  n_rep_b1b = 0;
} else {
  n_rep_b1a = 0;
  n_rep_b1b = 1;
}

```

(this code was not tested and I don’t program in javascript so it may not work or at least be suboptimal!)  
Then in your loops that control whether given block is presented you would put in the nReps field:

- for `B1a` block loop: `n_rep_b1a`
- for `B1b` block loop: `n_rep_b1b`
