# Random positions

**URL:** <https://discourse.psychopy.org/t/random-positions/3682>\
**Category:** Builder\
**Created:** [January 3, 2018, 4:02pm UTC](https://discourse.psychopy.org/t/random-positions/3682 "2018-01-03T16:02:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vmp](https://avatars.discourse-cdn.com/v4/letter/v/85f322/32.png) [@vmp](https://discourse.psychopy.org/u/vmp)\
**Post date:** [January 3, 2018, 4:02pm UTC](https://discourse.psychopy.org/t/random-positions/3682/1 "2018-01-03T16:02:10Z")

</div>

**OS** : MAC 10.11.6  
**PsychoPy version** : v1.85.4  
**What are you trying to achieve?** : I’m trying to do the trail making test. I want circle from 1 to 10 and random positions in the screen.

To manage that, I decide to use semi random positions. That’s my code to obtain a list of random position

```auto
import pandas as pd
from random import shuffle

positions = ['(-.9, -.9)', '(-.9, -.65)', '(-.9, -.4)', '(-.9, -.15)', '(-.9, .1)', '(-.9, .35)', '(-.9, .6)', '(-.9, .85)',
             '(-.65, -.9)', '(-.65, -.65)', '(-.65, -.4)', '(-.65, -.15)', '(-.65, .1)', '(-.65, .35)', '(-.65, .6)', '(-.65, .85)',
             '(-.4, -.9)', '(-.4, -.65)', '(-.4, -.4)', '(-.4, -.15)', '(-.4, .1)', '(-.4, .35)', '(-.4, .6)', '(-.4, .85)',
             '(-.15, -.9)', '(-.15, -.65)', '(-.15, -.4)', '(-.15, -.15)', '(-.15, .1)', '(-.15, .35)', '(-.15, .6)','(-.15, .85)',
             '(.1, -.9)', '(.1, -.65)', '(.1, -.4)', '(.1, -.15)', '(.1, .1)', '(.1, .35)', '(.1, .6)', '(.1, .85)',
             '(.35, -.9)', '(.35, -.65)', '(.35, -.4)', '(.35, -.15)', '(.35, .1)', '(.35, .35)', '(.35, .6)',
             '(.35, .85)','(.6, -.9)', '(.6, -.65)', '(.6, -.4)', '(.6, -.15)', '(.6, .1)', '(.6, .35)', '(.6, .6)', '(.6, .85)',
             '(.85, -.9)', '(.85, -.65)', '(.85, -.4)', '(.85, -.15)', '(.85, .1)', '(.85, .35)', '(.85, .6)', '(.85, .85)'] #list of all the position possible in the screen with size (0.2, 0.2)

random.sample(positions, len(positions))

my_list_positions = random.sample(positions, 10) #select 10 unique position from the list of all possible possitions

circle = ['circle_1.jpeg', 'circle_2.jpeg', 'circle_3.jpeg', 'circle_4.jpeg', 'circle_5.jpeg',
          'circle_6.jpeg', 'circle_7.jpeg', 'circle_8.jpeg', 'circle_9.jpeg', 'circle_10.jpeg'] #image for the test

shuffle(circle)

df = pd.DataFrame(my_list_positions, columns=["positions"]) #create a dataframe
df['circle'] = circle #add the circles to a random position
df.to_csv('conditions.csv', index=False) #export in a csv file 

```

The first thing I tried was to loop in the conditions file, but then I realize that I want all my circle at the same time. So I added 10 different image in my trial instead of 1, the 10 images are the 10 different circle and for all the image I would gave it a different position.

The problem is that I always get an error that “positions” cannot be find.

Builder :

 ![37 AM](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/7/a/7a70bf8f2d65669cd12d47383f4097e73b652a9f.png)

I tried with $positions, positions, [positions], [$positions], [positions$], [positions]$

 ![33 AM](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/f/4/f41389708693b096ea999a7349506c045a64ecfa.png)

Thanks!!

---

<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:** [January 4, 2018, 1:50am UTC](https://discourse.psychopy.org/t/random-positions/3682/2 "2018-01-04T01:50:08Z")

</div>

- The position field is set to be constant. Therefore PsychoPy expects this variable to be created at the beginning of the experiment. If your code runs later (e.g. at the beginning of a routine), then that variable won’t exist at the start of the experiment. If you want the positions field to be updated on every trial (i.e. every report of the loop), then specify that instead.
- you just want one of the positions from your list for each stimulus, so specify that by its index (beginning at 0), e.g. `my_list_positions[0]` through `my_list_positions[9]` for the respective stimuli.

---

<div class="post-metadata">

**Author:** ![vmp](https://avatars.discourse-cdn.com/v4/letter/v/85f322/32.png) [@vmp](https://discourse.psychopy.org/u/vmp)\
**Post date:** [January 5, 2018, 4:10pm UTC](https://discourse.psychopy.org/t/random-positions/3682/3 "2018-01-05T16:10:17Z")

</div>

I change my mind. Finally, I decide to use the index without passing by a condition file.  
My code :

```auto
positions = [[-.9, -.9], [-.9, -.65], [-.9, -.4], [-.9, -.15], [-.9, .1], [-.9, .35], [-.9, .6],[-.9, .85],
             [-.65, -.9], [-.65, -.65], [-.65, -.4], [-.65, -.15], [-.65, .1], [-.65, .35], [-.65, .6], [-.65, .85],
             [-.4, -.9], [-.4, -.65], [-.4, -.4], [-.4, -.15], [-.4, .1], [-.4, .35], [-.4, .6], [-.4, .85],
             [-.15, -.9], [-.15, -.65], [-.15, -.4], [-.15, -.15], [-.15, .1], [-.15, .35], [-.15, .6], [-.15, .85],
             [.1, -.9], [.1, -.65], [.1, -.4], [.1, -.15], [.1, .1], [.1, .35], [.1, .6], [.1, .85],
             [.35, -.9], [.35, -.65], [.35, -.4], [.35, -.15], [.35, .1], [.35, .35], [.35, .6], [.35, .85],
             [.6, -.9], [.6, -.65], [.6, -.4], [.6, -.15], [.6, .1], [.6, .35], [.6, .6], [.6, .85],
             [.85, -.9], [.85, -.65], [.85, -.4], [.85, -.15], [.85, .1], [.85, .35], [.85, .6], [.85, .85]]

shuffle(positions) 

```

I gave each image an index position

And then in each frame

```auto
 if mouse.isPressedIn(cercle_1):
    thisExp.addData('correct', 1)
    thisExp.addData('RT', t)
    continueRoutine = False

elif mouse.isPressedIn(cercle_2):
    thisExp.addData('correct', 0)

elif mouse.isPressedIn(cercle_3):
    thisExp.addData('correct', 0)

```

To be easier to create, I will use only 3 circles at this time and add the others when it will be working.

Problem : I want to change the image if cercle\_2 or cercle\_3 is pressed. I tried to add red\_2 as an image component and then code that now replace cercle\_2 by red\_2, but I’m not able to elaborate something

Thanks!!
