#### Howdy, Stranger!

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

Supported by

# [solved] Controlling the randomisation of loop variables

edited July 2015

Let's say I want a loop which goes through 6 items (A, B, C, D, E and F) randomly. However, there is a constraint that the first item in the loop should be A, B or C and the last item in the loop should be D, E or F; then the middle four elements are random (modulo the items that have already been chosen at the terminals).

How could this be done in OpenSesame?

My understanding is that I would have to split it up into three loops: one for the first element; another for the middle four; and a final one for the last element. I would then have to code some logic into the loop sequence items to ensure items aren't picked more than once (e.g., I could use subject number modulo 3 to determine the first and last, thus knowing what's remaining in the middle; or a permutation matrix generated from a static random seed).

Thanks;
C

• edited 9:39AM

Hi,

As far as I see, there is no easy and direct way of doing this with the `loop` item. However, doing it with an `inline_script` is not much harder. I would do following. First, I'd put a small `inline_script` in the beginning of your `sequence`. Furthermore, in the `loop` item itself, you don't have to put your variable names (A,B,C,..), but just some sort of counter.
In the `inline_script`, you can put the actual part that selects the condition or item you need. Something like this, should do:

``````import random

# I assume earlier in the experiment you created a list with your items
# e.g. items = ['A','B','C','D','E','F']
# this has to be defined outside the loop, in order to make sure that no
# item does appear more than once
# counter can be the counter variable that is running in the `loop` item

if counter == 0:
cur_item = random.choice(['A','B','C'])
items.remove(cur_item)
elif counter == 5: # (or whatever the index of the last element is)
cur_item = random.choice(['D','E','F'])
items.remove(cur_item)
else:
cur_item = items.pop(0)

# anything else that you want to do
``````

Does this do what you had in mind?

Good luck,

Eduard

• edited 9:39AM

Thanks, Eduard

Yes, this looks like it should work Now I've learned that what is declared in inline scripts persists until the end of the experiment it should, like you say, be very easy to get things to run the way I have in mind.

• edited 9:39AM

Great to hear. Then I'll mark this discussion as solved. If you happen to have more question don't hesitate to post them again in here.

Eduard