# Simultaneously randomize two columns in conditions file

**URL:** <https://discourse.psychopy.org/t/simultaneously-randomize-two-columns-in-conditions-file/22635>\
**Category:** Builder\
**Created:** [April 27, 2021, 6:32pm UTC](https://discourse.psychopy.org/t/simultaneously-randomize-two-columns-in-conditions-file/22635 "2021-04-27T18:32:26Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![helenr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/helenr/32/15631_2.png) [@helenr](https://discourse.psychopy.org/u/helenr)\
**Post date:** [April 27, 2021, 6:32pm UTC](https://discourse.psychopy.org/t/simultaneously-randomize-two-columns-in-conditions-file/22635/1 "2021-04-27T18:32:26Z")

</div>

**What are you trying to achieve?:**

I’m trying to randomize two columns in my conditions file, one sound and one image, so that they do not always occur in the same pairs.

E.g. sound 1, 2, 3, 4 and image a, b, c, d not always occurring in the combinations 1-a, 2-b, 3-c, 4-d.

**What did you try to make it work?:**

I know this is a issue that has been brought up multiple times in the forum.

I have tried the 2012 solution offered here with the following code:  
[https://groups.google.com/g/psychopy-users/c/WljgXeoy6Ik](https://groups.google.com/g/psychopy-users/c/WljgXeoy6Ik)

import random  
import csv

first=[1,2,3,4]  
second=[“A”,“B”,“C”,“D”]

firstRand=random.sample(first,len(first))  
secondRand=random.sample(second,len(second))

with open(‘order\_file.csv’,‘wb’) as w:  
write=csv.writer(w)  
writer.writerow([‘first’,‘second’])  
for i in range(len(firstRand)):  
writer.writerow([firstRand[i],secondRand[i]])

**What specifically went wrong when you tried that?:**  
Not only does this solution not work for me (NameError: name ‘writer’ is not defined) but it’s also unclear to me how it is supposed to work.

I’m not using a csv file but an excel file as my conditions. Am I supposed to enter the names of my files in the first= and second= lists? I’m using over a 100 files so I’d like to avoid this.

I’m hoping there is a more recent solution to this problem, as I know it is a common one.

---

<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:** [April 27, 2021, 6:35pm UTC](https://discourse.psychopy.org/t/simultaneously-randomize-two-columns-in-conditions-file/22635/2 "2021-04-27T18:35:11Z")

</div>

Why do you have 100 spreadsheets?

---

<div class="post-metadata">

**Author:** ![helenr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/helenr/32/15631_2.png) [@helenr](https://discourse.psychopy.org/u/helenr)\
**Post date:** [April 27, 2021, 6:48pm UTC](https://discourse.psychopy.org/t/simultaneously-randomize-two-columns-in-conditions-file/22635/3 "2021-04-27T18:48:29Z")

</div>

Sorry, I was referring to over 100 sound files and images. Ideally, I’d use only one excel file.

---

<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:** [April 27, 2021, 7:41pm UTC](https://discourse.psychopy.org/t/simultaneously-randomize-two-columns-in-conditions-file/22635/4 "2021-04-27T19:41:46Z")

</div>

I’ve just written a demo called Independent Randomisation that should help.

> [@PsychoPy Online Demos](https://discourse.psychopy.org/t/psychopy-online-demos/22319):
>
> If you have routines or experiments that are likely to be useful to others, please create a post in this thread, editing your post to add new demos so there is one post per creator. Ideally set your demos so they can be tested without consuming a credit (if you don’t have a site license) by adding a final routine that can’t end, disabling save incomplete results and adding a small number of credits. Please do not discuss the demos in this thread or create more than one post unless you have a la…

---

<div class="post-metadata">

**Author:** ![helenr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/helenr/32/15631_2.png) [@helenr](https://discourse.psychopy.org/u/helenr)\
**Post date:** [April 28, 2021, 7:59am UTC](https://discourse.psychopy.org/t/simultaneously-randomize-two-columns-in-conditions-file/22635/5 "2021-04-28T07:59:10Z")

</div>

Thanks very much for creating this demo!! I’m sure it will help many other users too.

I’ve got a question and am receiving an error message when trying it out.

First, my error:

listA.append(str(int(grid)))  
ValueError: invalid literal for int() with base 10: ‘myimage.png’

I suppose I’m receiving this error because the names of png files that the code is reading in are not simply integers. Is there a way to solve this without changing all of my file names?

Now the question:

Just to be sure, I don’t need the code in the ‘trial’ routine, do I? That is simply to rate the answers as correct or incorrect and give feedback at the end, right?

Thanks again for your help.

EDIT:

I changed the code to

listA.append(str(str(grid)))

and now I can run the experiment without an error message. I’ll leave the error message in case it helps anyone reading this in the future.

---

<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:** [April 28, 2021, 8:29am UTC](https://discourse.psychopy.org/t/simultaneously-randomize-two-columns-in-conditions-file/22635/6 "2021-04-28T08:29:16Z")

</div>

Sorry for the confusion. My code was coping with translating numbers to strings without ending .0. You just need listA.append(grid)

In the trial routine you do need thisA=listA.pop() and thisExp.addData(‘thisA’,thisA) to select and record the value being used.

---

<div class="post-metadata">

**Author:** ![helenr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/helenr/32/15631_2.png) [@helenr](https://discourse.psychopy.org/u/helenr)\
**Post date:** [April 28, 2021, 9:51am UTC](https://discourse.psychopy.org/t/simultaneously-randomize-two-columns-in-conditions-file/22635/7 "2021-04-28T09:51:58Z")

</div>

So, it is kind of working for me - the sound and image do not appear in the same combinations anymore. 🥳

However, the images (grid - what I put in the place of your colA) always occurs in the same order.  
I suppose this is simply how your code works (while the one column is randomized, the other is not thus resulting in random pairs)?

While this is better than before, it is not ideal for the purposes of my experiment. Is there anyway to randomize colA as well, but separately from colB?

---

<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:** [April 28, 2021, 9:58am UTC](https://discourse.psychopy.org/t/simultaneously-randomize-two-columns-in-conditions-file/22635/8 "2021-04-28T09:58:20Z")

</div>

Check that both of your loops are set to random. Mine are.

---

<div class="post-metadata">

**Author:** ![helenr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/helenr/32/15631_2.png) [@helenr](https://discourse.psychopy.org/u/helenr)\
**Post date:** [April 28, 2021, 10:01am UTC](https://discourse.psychopy.org/t/simultaneously-randomize-two-columns-in-conditions-file/22635/9 "2021-04-28T10:01:17Z")

</div>

Yes, all of my loops are set to random.

I also tried using your experiment and simply loading in my conditions file and changing colA to grid, etc. and have the same result.

---

<div class="post-metadata">

**Author:** ![helenr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/helenr/32/15631_2.png) [@helenr](https://discourse.psychopy.org/u/helenr)\
**Post date:** [April 28, 2021, 10:07am UTC](https://discourse.psychopy.org/t/simultaneously-randomize-two-columns-in-conditions-file/22635/10 "2021-04-28T10:07:06Z")

</div>

Sorry - false alarm!

For whatever reason, it’s working now.

Thank you very much!
