Mindstorms - want to perform various tasks, but never two of the same in a row (and other restrictions) I want to make a program that does a few things in a random order: I have this so far:

The problem here is... I have set the switch block with the random number block to 3 different programs to run. So of course they run randomly. But I want to make it like this: When program 1 on the switch block has finished the random number cannot be 1. 2 can run twice but not three times, the same for 3. 2 can never run after 3 or 3 after 2. So when 2 finished the program, all of the numbers can be the case. If 2 comes after two, the next step should be 1. Not 3 because I made number 3 the reversed of 2 which I don't want to make run after eachother.
This is an example of how it should act in the loop that's going 6 times:
This looks like a state machine.

Save the "state" of your program in a variable. The state will be which program to run, so 1, 2 or 3.

You will also need some more variables to track the previous state and how many times a program has run.

Code flow will be something like this:

next_state = 1
prev_state = 0
run_count = 0
begin loop
    compute_next_state
    switch next_state
        case 1: run program_1
        case 2: run program_2
        case 3: run program_3
    end switch
    if next_state == prev_state
        run_count = run_count + 1
    else
       run_count = 0
    end if
    prev_state = next_state
end loop


I left out the implementation of compute_next_state since you have that spelled out quite well in your diagram.