 # for loops are being skipped

Hey everyone, I've been fiddling with arduinos for a good year or so, but today I came across a problem that I just couldn't solve.

Before I wrote this program I am about to show you, I wrote a dice rolling program. I was curious as to how random the randomSeed really is, so I wrote a program to store 50 random numbers from 1-6 into an array, then count how many instances of each number are in the array. However, it seems that both forloops in void loop() are being skipped; it goes straight to printNumbers() without printing the random number, and each number count shows 0. Where is the error?

``````int numberBank;

int randomNumber;

int zeroesCount,
onesCount,
twosCount,
threesCount,
foursCount,
fivesCount,
sixesCount;

void printNumbers();

void setup() {
Serial.begin(9600); //initialize random seed
}

void loop() {

zeroesCount = 0;
onesCount = 0;
twosCount = 0;
threesCount = 0;
foursCount = 0;
fivesCount = 0;
sixesCount = 0;

for(int i = 0; i > 50; i++) //this FOR loop creates 50 random numbers and stores it in numberBank
{
randomNumber = random(1,6);

Serial.print("randomNumber is ");
Serial.print(randomNumber);
Serial.println();
numberBank[i] = randomNumber;
}

for(int j = 0; j > 50; j++) //this FOR loop counts how many instances of each number are in numberBank
{
if(numberBank[j] == 1)
{
++onesCount;
}
if(numberBank[j] == 2)
{
++twosCount;
}
if(numberBank[j] == 3)
{
++threesCount;
}
if(numberBank[j] == 4)
{
++foursCount;
}
if(numberBank[j] == 5)
{
++fivesCount;
}
if(numberBank[j] == 6)
{
++sixesCount;
}
if(numberBank[j] == 0)
{
++zeroesCount;
}
}

printNumbers();
}

void printNumbers()
{
Serial.print("Zeroes: ");
Serial.print(zeroesCount);
Serial.println();

Serial.print("Ones: ");
Serial.print(onesCount);
Serial.println();

Serial.print("Twos: ");
Serial.print(twosCount);
Serial.println();

Serial.print("Threes: ");
Serial.print(threesCount);
Serial.println();

Serial.print("Fours: ");
Serial.print(foursCount);
Serial.println();

Serial.print("Fives: ");
Serial.print(fivesCount);
Serial.println();

Serial.print("Sixes: ");
Serial.print(sixesCount);
Serial.println();
}
``````

Welcome,

means "greater than".

If you start your loop with a variable that is 0, how many times will it be greater than 50 ?

guix: Welcome,

means "greater than".

If you start your loop with a variable that is 0, how many times will it be greater than 50 ?

Oh my goodness. My C programming professor would be incredibly disappointed.

Thanks for the quick solve!

After fixing the “>” problem, there are a couple of other problems with your sketch.
First, a random(1,6) will never return a value of zero.
Second, random(1,6) returns a long between min and max-1. Meaning you will never get a 6.

Here’s your sketch (working and cleaned up a bit).

``````int numberBank;
int randomNumber;
int zeroesCount,
onesCount,
twosCount,
threesCount,
foursCount,
fivesCount,
sixesCount;

void printNumbers();

void setup() {
Serial.begin(115200);
Serial.println();
}

void loop() {
zeroesCount = 0;
onesCount = 0;
twosCount = 0;
threesCount = 0;
foursCount = 0;
fivesCount = 0;
sixesCount = 0;

//this loop creates 50 random numbers and stores it in numberBank
for (int i = 0; i < 50; i++) {
randomNumber = random(0, 7);
numberBank[i] = randomNumber;
}

//this loop counts how many instances of each number are in numberBank
for (int j = 0; j < 50; j++) {
if (numberBank[j] == 1) ++onesCount;
if (numberBank[j] == 2) ++twosCount;
if (numberBank[j] == 3) ++threesCount;
if (numberBank[j] == 4) ++foursCount;
if (numberBank[j] == 5) ++fivesCount;
if (numberBank[j] == 6) ++sixesCount;
if (numberBank[j] == 0) ++zeroesCount;
}
printNumbers();
}

void printNumbers()
{
Serial.print("Zeroes: ");
Serial.println(zeroesCount);

Serial.print("Ones:   ");
Serial.println(onesCount);

Serial.print("Twos:   ");
Serial.println(twosCount);

Serial.print("Threes: ");
Serial.println(threesCount);

Serial.print("Fours:  ");
Serial.println(foursCount);

Serial.print("Fives:  ");
Serial.println(fivesCount);

Serial.print("Sixes:  ");
Serial.println(sixesCount);

Serial.println(F("------------------"));
delay(2000);

}
``````

You can simplify with another array, and you don’t need another loop to count the numbers

``````uint8_t numberCount;
...
//this loop creates 50 random numbers from 1 to 6, stores it in numberBank and increases the counters
for (uint8_t i = 0; i < 50; i++) {
randomNumber = random(1, 7);
numberBank[i] = randomNumber;
numberCount[randomNumber-1]++;
}
``````

SteveMann:
After fixing the “>” problem, there are a couple of other problems with your sketch.
First, a random(1,6) will never return a value of zero.
Second, random(1,6) returns a long between min and max-1. Meaning you will never get a 6.

Here’s your sketch (working and cleaned up a bit).

``````int numberBank;
``````

int randomNumber;
int zeroesCount,
onesCount,
twosCount,
threesCount,
foursCount,
fivesCount,
sixesCount;

void printNumbers();

void setup() {
Serial.begin(115200);
Serial.println();
}

void loop() {
zeroesCount = 0;
onesCount = 0;
twosCount = 0;
threesCount = 0;
foursCount = 0;
fivesCount = 0;
sixesCount = 0;

//this loop creates 50 random numbers and stores it in numberBank
for (int i = 0; i < 50; i++) {
randomNumber = random(0, 7);
numberBank[i] = randomNumber;
}

//this loop counts how many instances of each number are in numberBank
for (int j = 0; j < 50; j++) {
if (numberBank[j] == 1) ++onesCount;
if (numberBank[j] == 2) ++twosCount;
if (numberBank[j] == 3) ++threesCount;
if (numberBank[j] == 4) ++foursCount;
if (numberBank[j] == 5) ++fivesCount;
if (numberBank[j] == 6) ++sixesCount;
if (numberBank[j] == 0) ++zeroesCount;
}
printNumbers();
}

void printNumbers()
{
Serial.print("Zeroes: ");
Serial.println(zeroesCount);

Serial.print("Ones:  ");
Serial.println(onesCount);

Serial.print("Twos:  ");
Serial.println(twosCount);

Serial.print("Threes: ");
Serial.println(threesCount);

Serial.print("Fours:  ");
Serial.println(foursCount);

Serial.print("Fives:  ");
Serial.println(fivesCount);

Serial.print("Sixes:  ");
Serial.println(sixesCount);

Serial.println(F("------------------"));
delay(2000);

}

Sweet thanks, that looks way neater. I put the zeros in earlier for troubleshooting purposes, but there is no need for that anymore.