#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

# [open] How to randomly choose two positions from eight?

edited December 2013

I need to randomly choose two positions from eight to add green.png, and the other six positions to add red.png, so that the sketchpad has two greens and six reds. how to achieve this ? The eight positions are changeless, for instance,
{0 128.0; 90.5 90.5; 128 0; 90.5 -90.5; 0 -128; -90.5 -90.5; -128 0; -90.5 90.5}.

Thank you.

• edited 4:41AM

Hi,

You could determine the red and green positions at the beginning of every trial by placing something like the following in the Prepare phase tab of an inline_script item, appended to the beginning of your trial sequence:

``````# Import modules:
from openexp.canvas import canvas
import random

# Create list containing all 8 posible (x,y) positions:
pos_list = [[0, 128.0],[90.5,90.5],[128,0], [90.5,-90.5], [0,-128],
[-90.5,-90.5],[-128,0],[-90.5,90.5]]

# Shuffle the list:
random.shuffle(pos_list)

# Use the built-in method `pop()` to draw items from a list WITHOUT replacement.
# This way, you'll be sure that your green item will not be overwritten by a red one.

# Determine two positions for the green items:
xGreen1, yGreen1 = pos_list.pop()
xGreen2, yGreen2 = pos_list.pop()

# The remainings items are for the red items:
xRed1, yRed1 = pos_list.pop()
# etc.

# Don't forget to 'set' your variables for future use in the GUI
# (e.g. the logger item):
exp.set("xGreen1", xGreen1)
# etc.

``````

Does this help?

Best,

Lotje

Did you like my answer? Feel free to