Random Positioning of Stimulus

Hello, I am trying to get a circle stimulus to appear on random points within bounds on the screen. Currently, I have:

target = psychopy.visual.Circle(win, units="pix", radius=10, fillColor = [-1,-1,-1], lineColor=[-1,-1,-1], edges=99, pos=(-5, 5))
target_starting_positions = [[0, -0.5], [-0.5, -0.5], [0.5, -0.5]]
rand_pos = shuffle(target_starting_positions)

However, the circle just shows up in the middle of the screen and does not randomize. Any help would be appreciated!

I think you want your final line to index the first element rather than pop an item off your list

Try target.setPos(rand_pos[0])

Note that

rand_pos = shuffle(target_starting_positions)

won’t assign anything to rand_pos, as shuffle() doesn’t return a value. It just acts directly on its argument, like this:


Why? I don’t know.

Having said that, target_starting_positions should still get shuffled, and the position of your target stimulus should therefore be randomly selected, regardless of whether you use .pop() or get the first element from the list.

I’d suggest you put in some (temporary) debugging code like:


to see if the assignment works as expected. That will help you nail down the issue (eg does it get re-initialised somewhere later before being drawn?)

Unfortunately, that did not work in resetting the circle stimulus. As Michael suggested, I put a debugging code, and it does print a new position, but it is not reflected in the task itself.

That’s a great idea, thank you. So I added


and it does print a different starting position every time I run the task, but it’s not getting reflected in the task. The circle stimulus is still stagnant in the center of the screen.

OK, so somewhere between the code above and when the stimulus actually gets drawn, either the position of that stimulus is being reset to [0, 0], or the stimulus gets re-initialised (with the default position of [0, 0]), or it never gets drawn at all, but another similar stimulus does, and so on.

You need to work through your code, following the chain from the portion above (which is clearly working, as your debugging print statement shows), to the place where it actually gets drawn.

e.g. You could short-circuit the process by seeing what happens if, immediately after the code above, you simply put:


Do you see the stimulus at the expected random location, but also another one at the centre?

Lastly, just to verify, is this all actually happening in a script written from scratch, or is this a Builder experiment, with these snippets of code in code components? In the latter case, it is very easy to get conflicts between stimuli defined in code and those created through graphical components.

I figured this out by doing this:

target = psychopy.visual.Circle(win, units='height', radius=0.01, fillColor = [-1,-1,-1], lineColor=[-1,-1,-1], edges=99)
tlower_bound_x = -0.1
tupper_bound_x = 0.1
tlower_bound_y = 0.2
tupper_bound_y = 0.4
target_x = np.random.uniform(tlower_bound_x, tupper_bound_x)
target_y = np.random.uniform(tlower_bound_y, tupper_bound_y)