# Randomisation without consecutive presentations

**URL:** <https://discourse.psychopy.org/t/randomisation-without-consecutive-presentations/8089>\
**Category:** Builder\
**Tags:** randomization\
**Created:** [June 12, 2019, 3:14am UTC](https://discourse.psychopy.org/t/randomisation-without-consecutive-presentations/8089 "2019-06-12T03:14:58Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [June 12, 2019, 5:14am UTC](https://discourse.psychopy.org/t/randomisation-without-consecutive-presentations/8089/2 "2019-06-12T05:14:13Z")

</div>

@Mirjam provides a general solution here:

> [@Randomize trial order without repeating stimuli twice in a row](https://discourse.psychopy.org/t/randomize-trial-order-without-repeating-stimuli-twice-in-a-row/7387/4):
>
> I don’t use Builder, but I have a piece of code (using numpy) at the start of an experiment that loops through my randomised list of stimuli to check for identical items in subsequent rows, and reshuffles the list (and checks again, and reshuffles if needed…) until there are none. This does introduce a short waiting time at the start, but never too long in my experience. # make sure the same target doesn't appear on consecutive trials doubletarget = False listok = False while listok == False: …

In your specific case, something like this might work:

- Insert a code component on your trial routine (from the “Custom” component panel).
- In its “begin experiment” tab, put something like this ( we need to run this at the start of the experiment since the code is non-deterministic: we don’t know how long it will take to run (realistically though, it will be a fraction of a second for only 12 entries):

```auto
stimulus_list = ['v', 'b', 'n', 'm'] * 3
shuffle(stimulus_list)

# make sure the same target doesn't appear on consecutive trials
double_stim = False
list_ok = False

while list_ok == False:
    for i in range(len(stimulus_list) - 1):
        # check for equal neighbours:
        if stimulus_list[i] == stimulus_list[i + 1]:
            double_stim = True # D'oh!
            break # can stop checking on this run
    if double_stim == True:
        shuffle(stimulus_list) # try a new order
        double_stim = False
    else:
        list_ok = True # Yay! The loop won't run again.

print(stimulus_list)

```

---

_[View the full topic](https://discourse.psychopy.org/t/randomisation-without-consecutive-presentations/8089)._
