psychopy.org | Reference | Downloads | Github

Pseudo_randomized stimuli order with two conditions

I have a list of stimuli, where each item is either congruent or incongruent (cond CO or IN) and each item is of type A, B or C (type A, B C)

So example:
item 1: CO, A
item 2: IN, A
item 3: CO, B
item 4: IN, C

I want to pseudo-randomize the order of this stimuli list so that:
1) the same type (A,B,C) does not repeat (e.g., I don’t want A,A,… or B,B,…) in a row
and at the same time so that:
2) the same cond (CO, IN) does not repeat more than twice in a row (e.g., CO,CO,CO, IN, IN, IN, …)

I have always used this handy randomization function to fulfill the first condition.
My question is: How could the function be modified to fulfill both conditions at the same time:

# RANDOMIZATION FUNCTIONS
#helper function for standard_randomization (Given a list, find first pair that does not contain given condition.)
def find_indelst, condition):
    a = 'a'
    b = 'b'
    idx = 0
    while a != b and idx < len(lst):
        a = idx
        b = idx+1
        idx += 1
    # assuming pair is always found!! it might not be the case
    return idx+1

#Randomization function (input: a list of dictionaries, returns a randomized list)
def standard_randomization(lst):
    randomized_lst = []
    pre_item = ''
    # first item
    item = lst.pop(lst.inderandom.choice(lst)))# get a random item from list
    randomized_lst.append(item)
    pre_cond = item[CONDITION]
    while len(lst) > 1:
        item = lst.pop(lst.inderandom.choice(lst)))
        if pre_cond == item[CONDITION]:
            lst.append(item)
        else:
            randomized_lst.append(item)
            pre_cond = item[CONDITION]
    # check that n and n-1 are not the same
    item = lst[0]
    if pre_cond != item[CONDITION]:
        randomized_lst.append(item)
    # find place to insert last item if they are
    else:
        idx = find_inderandomized_lst, item[CONDITION])
        randomized_lst.insert(idx, item)    
    return randomized_lst

I have thought of adding the CONDITION2 into this function and checking for pre-cond and pre.pre-cond item for the second cond. Does anyone have more elegant idea how to go around it?

Thanks for any suggestions,

Is CO/IN simply alternating?

For ABC do you also want equal numbers of each type or are you happy with random variations from that?

If you have a number 0, 1, or 2 and want the next number to be from the same range but not identical then you can randomly add 1 or 2 and take the modulus %3 or the result.

Also, have a look at my online demos post which I’ve moved to the blog category for an example of something similar.

In every list I have always 27 items. There is an equal number of A, B and C in a list (9 each).
Rougly half of A is CO (either 13 or 14) and half IN, the same for B and C.

I need to randomize each of these lists with the two described conditions.

A friend of mine suggested to create a state variable with 4 possible states (CON1, CON2, IN1, IN2) depending on the state, allowed options would be restricted.

I am not sure how to program the function so that it does not get stuck with e.g., three CO or two A at the end of a list.

I will take a look at your demos!

Thank you,

Can ACo follow BCo?

In my demo I add an extra trial if the experiment would otherwise be forced to repeat.