How to set trials to semi-random order?

I am building a modified version of the BART (balloon risk taking task) using the demo from Pavlovia. I have two different colored balloons that build up credit for two different gift cards.

This is my conditions file:

Basically, I don’t want to have like 8 balloons of the same color to show up in a row. Ideally, maxPumps would remain fully random, but I wouldn’t want more than 3 of the same colored balloon to be presented in a row.

I will be running this online so it needs to translate to Java.

In the past I set it up so that the two balloon conditions were their own separate loops with a big loop going around it, but there were many problems with that.

TIA (:

You might find my Complex Randomisation demo useful

For example you could have a list of runs for each colour, such as

runs1 = [3,3,3,2,2,2,2,1,1,1]
shuffle(runs1)
rows1 = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
shuffle(rows1)

runs2 = [3,3,3,2,2,2,2,1,1,1]
shuffe(runs2)
rows2 = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
shuffle(rows2)

startRow = [0,20]
shuffle(startRow)
useRows = []

for Idx in range(10):
     for Jdx in range(runs1.pop()):
          useRows.append(rows1.pop()+startRow[0])
     for Jdx in range(runs2.pop()):
          useRows.append(rows2.pop()+startRow[1])

Then set your main loop to use useRows sequentially.