I’m going to guess that you want to run this randomly on each session, rather than have a pre-specified order in a conditions file. If so, the loop settings won’t be able to handle this for you, it will require some custom code. So I’d suggest you just have a loop set to run the required number of trials, and containing both A and B trial routines. Insert a code component on the first one, and in its “begin routine” tab, put something like this (this is Python code, you’ll need to translate it to JavaScript to run online):

```
# use your actual values:
p_A_given_B = 0.7
p_B_given_B = 1.0 - p_A_given_B # not used, just being explicit
p_B_given_A = 0.4
p_A_given_A = 1.0 - p_B_given_A # not used, just being explicit
p = random() # between 0 and 1
if your_loop_name.thisN == 0: # dummy value needed for first trial
last_type = np.random.choice(['A', 'B'])
if last_type == 'A':
if p < p_B_given_A:
current_type = 'B'
else:
current_type = 'A'
elif p < p_A_given_B:
current_type = 'A'
else:
current_type = 'B'
last_type = current_type
# store stuff in the data so you can test this is doing the right thing:
thisExp.addData('last_type', last_type)
thisExp.addData('p', p)
thisExp.addData('current_type', current_type)
# now only run this routine if it is the correct type:
if current_type == 'B':
continueRoutine = False
```

Then in the “B” routine, insert a code component and put something like this in its “begin routine” tab:

```
if current_type == 'A':
continueRoutine = False
```

The code above **REALLY NEEDS TO BE TESTED.** It is very easy to get this sort of nested boolean logic wrong, but hopefully this at least shows the sort of approach to take.