<p>This looks like a <a href="https://en.wikipedia.org/wiki/Finite-state_machine" rel="nofollow">state machine</a>.</p>

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

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

<p>Code flow will be something like this:</p>

<pre><code>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
</code></pre>

<p>I left out the implementation of <code>compute_next_state</code> since you have that spelled out quite well in your diagram.</p>

CC BY-SA 3.0
2016-02-15T00:04:08.563Z
I'm not sure how to use this code... I am not very skilled in coding and coding with nxt I have 0.0 experience...
Hendriks3D
CC BY-SA 3.0
2016-02-14T23:25:48.830Z
2016-02-14T23:25:48.830Z
David Lechner
<p>To make it easier, I added an extra program to run which is the same as 2X program 2 or 3. Because Program 2 and 3 are 90 degree turns. So now it should be clearer what the idea is: <a href="https://i.sstatic.net/4IVLo.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/4IVLo.png" alt="enter image description here"></a></p>

<p>So the loop could start on program 1 and run 2, 3 or 4 after, but it could also start on 2, 3 or 4 on which the next step always program 1 is. It could stay running and keep taking infinite steps. So only after the first program is randomly chosen, the rest of the random chosen programs will be after program 1. The rest is in a logical order. After program 1 the system will randomly choose a new program for the next step that could be any of the steps exept program 1. So a long way of steps could look like this: Program 2 - Program 1 - Program 4 - Program 1 - Program 2 - Program 1 - Program 3 - Program 1 - Program 3 - Program 1 - Program 4 - Program 1 etc. etc. etc. The amount of steps that will be taken is the amount of times that the loop is set to. Right now I have made it in blocks. Very simple. Only the first two steps could be the same, but that's no problem to me.  :)
<a href="https://i.sstatic.net/23h1E.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/23h1E.png" alt="enter image description here"></a>
Even though I found it out myself I thank you very much!</p>

CC BY-SA 3.0
2016-02-15T01:15:25.397Z
2016-02-15T01:15:25.397Z
Hendriks3D
<p>I want to make a program that does a few things in a random order: I have this so far:<a href="https://i.sstatic.net/0vYKA.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/0vYKA.png" alt="enter image description here"></a></p>

<p>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:<a href="https://i.sstatic.net/EbJww.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/EbJww.png" alt="enter image description here"></a></p>

<mindstorms><nxt><programming>
Mindstorms - want to perform various tasks, but never two of the same in a row (and other restrictions)
Hendriks3D
# Mindstorms - want to perform various tasks, but never two of the same in a row (and other restrictions)

(Asked by: username_0 on 2016-02-14)

I want to make a program that does a few things in a random order: I have this so far:[![enter image description here](https://i.sstatic.net/0vYKA.png)](https://i.sstatic.net/0vYKA.png)

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:[![enter image description here](https://i.sstatic.net/EbJww.png)](https://i.sstatic.net/EbJww.png)

**Tags:** mindstorms, nxt, programming


---

## 2 answers

### Answer by username_1 on 2016-02-14. Score: 2

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.

> **Comments:**
> **username_0**: I'm not sure how to use this code... I am not very skilled in coding and coding with nxt I have 0.0 experience...  

---

### Answer by username_0 on 2016-02-15. Score: 0

To make it easier, I added an extra program to run which is the same as 2X program 2 or 3. Because Program 2 and 3 are 90 degree turns. So now it should be clearer what the idea is: [![enter image description here](https://i.sstatic.net/4IVLo.png)](https://i.sstatic.net/4IVLo.png)

So the loop could start on program 1 and run 2, 3 or 4 after, but it could also start on 2, 3 or 4 on which the next step always program 1 is. It could stay running and keep taking infinite steps. So only after the first program is randomly chosen, the rest of the random chosen programs will be after program 1. The rest is in a logical order. After program 1 the system will randomly choose a new program for the next step that could be any of the steps exept program 1. So a long way of steps could look like this: Program 2 - Program 1 - Program 4 - Program 1 - Program 2 - Program 1 - Program 3 - Program 1 - Program 3 - Program 1 - Program 4 - Program 1 etc. etc. etc. The amount of steps that will be taken is the amount of times that the loop is set to. Right now I have made it in blocks. Very simple. Only the first two steps could be the same, but that's no problem to me. :)
[![enter image description here](https://i.sstatic.net/23h1E.png)](https://i.sstatic.net/23h1E.png)
Even though I found it out myself I thank you very much!


---

