# Independently randomize a variable

**URL:** <https://discourse.psychopy.org/t/independently-randomize-a-variable/18726>\
**Category:** Builder\
**Created:** [December 6, 2020, 10:51am UTC](https://discourse.psychopy.org/t/independently-randomize-a-variable/18726 "2020-12-06T10:51:43Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Khailing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/khailing/32/12742_2.png) [@Khailing](https://discourse.psychopy.org/u/Khailing)\
**Post date:** [December 6, 2020, 10:51am UTC](https://discourse.psychopy.org/t/independently-randomize-a-variable/18726/1 "2020-12-06T10:51:44Z")

</div>

**OS** : Win10  
**PsychoPy version** 3.6.6  
Hi,

I am designing an experiment with few conditions by using builder. the experiment has few conditions

1. randomize the number variables
2. randomize the letter height of number shown.
3. Response when participants got the answer correctly.

My problem is the randomize the letter height. The letter height is shown in the condition file with number. However, the letter height did not randomize, it always paired with the randomized number.

I had attached few photos to have clearer picture:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/8/3/831eb4d260be34c327a60644e3e6d7536b80c3c5.png)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/e/f/efea7f2cefb2c70393db87be8248536f1958f176.png)

I had set the letter height to “set every repeat”, but it does not randomize independently.

Hopefully anyone may help with this by using builder mode.

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [December 7, 2020, 10:36am UTC](https://discourse.psychopy.org/t/independently-randomize-a-variable/18726/2 "2020-12-07T10:36:45Z")

</div>

Hello,

PsychoPy always randomizes row-wise. How many random sizes do want to realise? How many numbers do you want to show?

You could simply make combinations of all your numbers and all your sizes in your Excel-file and have that randomized by PsychoPy.

Best Jens

---

<div class="post-metadata">

**Author:** ![Khailing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/khailing/32/12742_2.png) [@Khailing](https://discourse.psychopy.org/u/Khailing)\
**Post date:** [December 7, 2020, 3:32pm UTC](https://discourse.psychopy.org/t/independently-randomize-a-variable/18726/3 "2020-12-07T15:32:14Z")

</div>

Thank you so much for your reply.

This is the excel file with my 3 parameters:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/e/7/e7cd6b2e629095b57466e5a056b1810d4fe1da93.png)

I need to randomize the number and size, whereas corrAns is for the response set for participants. As you suggested that make combination in Excel-file and have that randomized by PsychoPy, may I have more details about how to execute this?

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [December 7, 2020, 4:17pm UTC](https://discourse.psychopy.org/t/independently-randomize-a-variable/18726/4 "2020-12-07T16:17:00Z")

</div>

You need to more specific how you want to randomize numbers and sizes? The following example would show each number twice but each time in a different size

| number | size | corrAns |
| --- | --- | --- |
| 1 | 0.05 | space |
| 2 | 0.05 | space |
| 3 | 0.07 | space |
| 4 | 0.07 | space |
| 5 | 0.2 | space |
| 6 | 0.3 | space |
| 7 | 0.5 | space |
| 8 | 0.8 | space |
| 9 | 1 | space |
| 1 | 1 | space |
| 2 | 0.8 | space |
| 3 | 0.5 | space |
| 4 | 0.3 | space |
| 5 | 0.2 | space |
| 6 | 0.07 | space |
| 7 | 0.07 | space |
| 8 | 0.05 | space |
| 9 | 0.05 | space |

Or do you simply want to present each number once but the size randomly assigned to it? Do all sizes have to appear?

Best Jens

---

<div class="post-metadata">

**Author:** ![Khailing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/khailing/32/12742_2.png) [@Khailing](https://discourse.psychopy.org/u/Khailing)\
**Post date:** [December 8, 2020, 6:47am UTC](https://discourse.psychopy.org/t/independently-randomize-a-variable/18726/5 "2020-12-08T06:47:31Z")

</div>

yes, you are right!

Each number would only appear once but in a random sequence, and all sizes have to appear in a random sequence like your example.

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [December 8, 2020, 7:39pm UTC](https://discourse.psychopy.org/t/independently-randomize-a-variable/18726/6 "2020-12-08T19:39:08Z")

</div>

- Delete the “size” column from your spreadsheet.
- Insert a Code component (from the custom component panel).
- In its “Begin experiment” tab, insert something like this to create a new random order every time the experiment is run:

```auto
sizes = [0.05, 0.05, 0.07, 0.07, 0.2, 0.3, 0.5, 0.8, 1]
shuffle(sizes)

```

- In the “Begin routine” tab, put something like this to select the value for the current trial and make sure it is recorded in the data file (because it isn’t in the conditions file any more, you become responsible for manually recording it in the data):

```auto
size = sizes.pop()
thisExp.addData('size', size)

```

Make sure the code component appears _above_ any stimulus component that wants to use the `size` variable, so that the latter gets access to the current value.

---

<div class="post-metadata">

**Author:** ![Khailing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/khailing/32/12742_2.png) [@Khailing](https://discourse.psychopy.org/u/Khailing)\
**Post date:** [December 9, 2020, 2:18am UTC](https://discourse.psychopy.org/t/independently-randomize-a-variable/18726/7 "2020-12-09T02:18:31Z")

</div>

> [@Michael](#):
>
> ```auto
> size = sizes.pop()
> thisExp.addData('size', size)
> 
> ```

Thank you so much! The code is able to randomize the sizes now!

How about I would like to present the numbers more than once when I run the experiment? Like you have suggested, but the number would you in a random sequence and the sizes in a random sequence.

| number | size | corrAns |
| --- | --- | --- |
| 3 | 0.05 | space |
| 4 | 0.05 | space |
| 6 | 0.07 | space |
| 4 | 0.07 | space |
| 1 | 0.2 | space |
| 7 | 0.3 | space |
| 9 | 0.5 | space |
| 8 | 0.8 | space |
| 9 | 1 | space |
| 2 | 1 | space |
| 3 | 0.8 | space |
| 9 | 0.5 | space |
| 1 | 0.3 | space |
| 8 | 0.2 | space |
| 6 | 0.07 | space |
| 4 | 0.07 | space |
| 7 | 0.05 | space |
| 5 | 0.05 | space |

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [December 10, 2020, 10:51am UTC](https://discourse.psychopy.org/t/independently-randomize-a-variable/18726/8 "2020-12-10T10:51:51Z")

</div>

Hello,

to show your numbers more than once, you simply increase the number of loop repetitions from 1 to x. x being the number of iterations. Do you intend to have a specific distribution of sizes and numbers? Should each number occur in each size or is this of no relevance?

Best Jens

---

<div class="post-metadata">

**Author:** ![Khailing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/khailing/32/12742_2.png) [@Khailing](https://discourse.psychopy.org/u/Khailing)\
**Post date:** [December 10, 2020, 3:10pm UTC](https://discourse.psychopy.org/t/independently-randomize-a-variable/18726/9 "2020-12-10T15:10:46Z")

</div>

> [@JensBoelte](#):
>
> umber of loop repetition

Hi,

I had increased the nResp to 3 (as per picture), but all the number just showed once.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/a/3/a3e2450e19758eb858d0838cbb95424c7b98128a.jpeg)

There are no specific rules for it.

Regards,  
Khai Ling

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [December 10, 2020, 9:23pm UTC](https://discourse.psychopy.org/t/independently-randomize-a-variable/18726/10 "2020-12-10T21:23:26Z")

</div>

> [@Khailing](#):
>
> all the number just showed once.

You would also need to make the list of sizes grow by the same amount, as otherwise an error will occur when you try to pop the tenth size from the list, which is defined to only have 9 indices:

```
sizes = [0.05, 0.05, 0.07, 0.07, 0.2, 0.3, 0.5, 0.8, 1] * 3

```

That will give you a list of 27 elements, which should correspond to the 27 trials you get by repeating your nine row conditions file three times.

---

<div class="post-metadata">

**Author:** ![Khailing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/khailing/32/12742_2.png) [@Khailing](https://discourse.psychopy.org/u/Khailing)\
**Post date:** [December 11, 2020, 4:47am UTC](https://discourse.psychopy.org/t/independently-randomize-a-variable/18726/11 "2020-12-11T04:47:57Z")

</div>

Hi Jens,

I am so grateful and happy as all the problems are solved!

Thanks for being so helpful. Really thank you!

Best wishes,  
Khai Ling

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [December 11, 2020, 10:13am UTC](https://discourse.psychopy.org/t/independently-randomize-a-variable/18726/12 "2020-12-11T10:13:19Z")

</div>

Hello Khai Ling

@Michael deserves the credit 😉

Best Jens

---

<div class="post-metadata">

**Author:** ![Khailing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/khailing/32/12742_2.png) [@Khailing](https://discourse.psychopy.org/u/Khailing)\
**Post date:** [December 11, 2020, 1:35pm UTC](https://discourse.psychopy.org/t/independently-randomize-a-variable/18726/13 "2020-12-11T13:35:48Z")

</div>

Hi, @Michael.

Thank you so much. Both of you really helped me out for my 1st psychopy experiment designed.

Thanks a lot!
