user34344: First-time poster here...
I have the following code to randomize note values and store them in an array loop1[i], where i is a specific step in the loop:
nextNote = random(-2, 3);
note = note + nextNote;

loop1[i] = note;

while (loop1[i] == loop1[i - 1] ||
       loop1[i] == loop1[i - 2] ||
       note > 6 ||
       note < 0) {
  nextNote = random(-2, 3);
  note = note + nextNote;
  loop1[i] = note;
}

(I hope this is clear enough).
Now, it usually works to a certain point until the sketch loops forever in order to randomize an allowed number. Therefore, my questions are:
Running an ATmega 2560, is this actually a task too demanding for the processor or should I suspect that it's not working properly?
How can I optimize the code, but retain the conditions above in order to avoid the loop getting stuck?

user31105: Some reasons why it will not work:

My feeling is that i gets too big, i.e. it is higher than the length of the array you are using. That can cause crashes, or at least unpredicted results. You can easily check this by printing the value of i (and check against the maximum value).
I don't see problems with the while loop (although I would use brackets around note > 6 and around note < 0, but I don't think this is really needed.
If the value gets below 0 or higher than 6, still random values are added, assume the value is +10... then it might take a lot of time (if ever) for that value to reach max 6 again. The same is true for values < 0. Maybe you should never allow values of less than 0 and higher than 6 anyway, or making the random values in such a way they tend to go towards the average (3).

Tip to debug:
However, the easiest way to debug this is to print all relevant variables (e.g. nextNote, note, i, the last two notes loop[i-2], loop[i-1] and the current note loop[i]... Also print a line inside the while loop so you know when you skip it or not. This probably will give you enough information to see what is wrong.

user7508: I think you have a problem with your math here, not with your code. What
you are simulating is very similar to a 1-dimensional random
walk.
According to the linked MathWorld page, for such a process “the most
probable number of sign changes in a walk is 0”.
If, at some point in your program, the loop starts with note == 6, and
then it starts randomly going up, there is a sizable probability for it
to never go back to values below 7.

Edit: One possible solution could be to avoid the random-walk notion
and instead repeat the random choice always from the same starting
point.
int oldNote = note;  // starting point
do
    note = oldNote + random(-2, 3);
while (
    note == loop1[i - 1] ||       
    note == loop1[i - 2] ||       
    note > 6 ||                       
    note < 0);
loop1[i] = note;


34344: <p>First-time poster here...</p>

<p>I have the following code to randomize note values and store them in an array <code>loop1[i]</code>, where <code>i</code> is a specific step in the loop:</p>

<pre class="lang-cpp prettyprint-override"><code>nextNote = random(-2, 3);
note = note + nextNote;

loop1[i] = note;

while (loop1[i] == loop1[i - 1] ||
       loop1[i] == loop1[i - 2] ||
       note &gt; 6 ||
       note &lt; 0) {
  nextNote = random(-2, 3);
  note = note + nextNote;
  loop1[i] = note;
}
</code></pre>

<p>(I hope this is clear enough).</p>

<p>Now, it usually works to a certain point until the sketch loops forever in order to randomize an allowed number. Therefore, my questions are:</p>

<p>Running an ATmega 2560, is this actually a task too demanding for the processor or should I suspect that it's not working properly?</p>

<p>How can I optimize the code, but retain the conditions above in order to avoid the loop getting stuck?</p>
 31105: <p>Some reasons why it will not work:</p>

<ul>
<li><p>My feeling is that <code>i</code> gets too big, i.e. it is higher than the length of the array you are using. That can cause crashes, or at least unpredicted results. You can easily check this by printing the value of <code>i</code> (and check against the maximum value).</p></li>
<li><p>I don't see problems with the while loop (although I would use brackets around <code>note &gt; 6</code> and around <code>note &lt; 0</code>, but I don't think this is really needed.</p></li>
<li><p>If the value gets below 0 or higher than 6, still random values are added, assume the value is +10... then it might take a lot of time (if ever) for that value to reach max 6 again. The same is true for values &lt; 0. Maybe you should never allow values of less than 0 and higher than 6 anyway, or making the random values in such a way they tend to go towards the average (3).</p></li>
</ul>

<p>Tip to debug:</p>

<p>However, the easiest way to debug this is to print all relevant variables (e.g. <code>nextNote</code>, <code>note</code>, <code>i</code>, the last two notes <code>loop[i-2]</code>, <code>loop[i-1]</code> and the current note <code>loop[i]</code>... Also print a line inside the while loop so you know when you skip it or not. This probably will give you enough information to see what is wrong.</p>
 7508: <p>I think you have a problem with your math here, not with your code. What
you are simulating is very similar to a <a href="http://mathworld.wolfram.com/RandomWalk1-Dimensional.html" rel="nofollow noreferrer">1-dimensional random
walk</a>.
According to the linked MathWorld page, for such a process “the most
probable number of sign changes in a walk is 0”.</p>

<p>If, at some point in your program, the loop starts with <code>note == 6</code>, and
then it starts randomly going up, there is a sizable probability for it
to never go back to values below 7.</p>

<hr>

<p><strong>Edit</strong>: One possible solution could be to avoid the random-walk notion
and instead repeat the random choice always from the same starting
point.</p>

<pre class="lang-c++ prettyprint-override"><code>int oldNote = note;  // starting point
do
    note = oldNote + random(-2, 3);
while (
    note == loop1[i - 1] ||       
    note == loop1[i - 2] ||       
    note &gt; 6 ||                       
    note &lt; 0);
loop1[i] = note;
</code></pre>
