random() function generates number that exceeds the given maximum limit

Good Day! I’m a Computer Engineering student and I have a question regarding this set of codes that we’ve been working with. Just like what I said in the title, I’m having problems with the random() function, I’ve already set the minimum and maximum values for it to generate but when I try to monitor them in the Serial Monitor, it still displays values that exceeds it. The confusing part is, when I run the code alone, it will function properly, but when i’m trying to combine it with my other codes which is the other parts of the project, it will malfunction.

I badly need help since the deadline for this is on the 21st of July, and I don’t know what’s the problem.

Thanks!

P.S. I don’t know how to place the code here like a displayed notepad or something, but I just attached it, sorry for the inconvenience.

keypad+random.txt (4.78 KB)

Use the </> icon in the posting menu to attach your code.

Have you used Serial.print(yourVariable); to help debug this problem?

.

Oh, thanks’ for the info, and yes, I’ve been using it to monitor the problem and trying to do some adjustments but it really doesn’t work like when I run it alone.

Here’s the code.

#include <Keypad.h>
#include <LiquidCrystal.h>

const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS]={
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
char number2[ROWS]={';','a','b','c'};
char number3[ROWS]={';','d','e','f'};
char number4[ROWS]={';','g','h','i'};
char number5[ROWS]={';','j','k','l'};
char number6[ROWS]={';','m','n','o'};
char number7[ROWS]={';','p','q','r'};
char number8[ROWS]={';','s','t','u'};
char number9[ROWS]={';','v','w','x'};
char number0[ROWS]={';','y','z','-'};
int output[] = {};
byte rowPins[ROWS]={5,4,3,2};
byte colPins[COLS]={8,7,6};
int keyDec;
int cursorposition=0;
int counter=1;
char prevkey;
int timer;
char prevkey1;
bool startdone = false;
bool gameover = false;
int value;
bool duplicateFound;
int duplicateCounter;
Keypad keypad = Keypad{makeKeymap(keys),rowPins,colPins,ROWS,COLS};
LiquidCrystal lcd(14,15,16,17,18,19);
void setup() {
  Serial.begin(9600);
  lcd.begin(16,2);
  lcd.print("Input name:");
  lcd.setCursor(0,1);
  
}


void loop() {
  while (startdone != true)
  {
    start();
  }
  lcd.setCursor(0,0);
  lcd.print("Welcome!");
  delay(3000);
  lcd.clear();

  while (gameover != true)
  {
    value = analogRead(A0);
  if ( value <= 400 )
  {
    for ( int a = 0; a <= 2 ; a++ )
    {
      duplicateFound = true;
      while ( duplicateFound == true )
      {
        output[a] = random(1,10);
        duplicateFound = false;
        for ( int b = 0 ; b < a ; b++ )
        {
          if ( output[a] == output[b] )
          {
            duplicateFound = true;
            duplicateCounter++;
          }
        }
      }
    }
    for ( int x = 0 ; x <= 2 ; x++ )
    {
      Serial.println(output[x]);
    }
    Serial.println(value);
    delay(2000);
  }
  
  else if ( value >= 401 && value <= 900 )
  {
    for ( int c = 0; c <= 3 ; c++ )
    {
      duplicateFound = true;
      while ( duplicateFound == true )
      {
        output[c] = random(1,10);
        duplicateFound = false;
        for ( int d = 0 ; d < c ; d++ )
        {
          if ( output[c] == output[d] )
          {
            duplicateFound = true;
            duplicateCounter++;
          }
        }
      }
    }
    for ( int y = 0 ; y <= 3 ; y++ )
    {
      Serial.println(output[y]);
    }
    Serial.println(value);
    delay(2000);
  }
  else if ( value > 900 )
  {
    for ( int e = 0; e <= 4 ; e++ )
    {
      duplicateFound = true;
      while ( duplicateFound == true )
      {
        output[e] = random(1,10);
        if (output[e] > 10)
          duplicateFound = true;
        duplicateFound = false;
        for ( int f = 0 ; f < e ; f++ )
        {
          if ( output[e] == output[f] )
          {
            duplicateFound = true;
            duplicateCounter++;
          }
        }
      }
    }
    for ( int z = 0 ; z <= 4 ; z++ )
    {
      Serial.println(output[z]);
    }
    Serial.println(value);
    delay(2000);
  }
  gameover = false;
  }
}

void start()
{
  char key = keypad.getKey();
if(key != NO_KEY)
  {
    if(counter==3)
    {
      counter=1;
      if ( key != prevkey || key != prevkey1 )
      {
        counter = 1 ;
        cursorposition++;
      }
    }
    else if(prevkey==key)
    {
      counter++;
    }
    else if(prevkey==NULL)
    {
      cursorposition=0;
    }
    else if(key!=prevkey || key != prevkey1)
    {
      counter=1;
      cursorposition++;
    }
    prevkey1 = key;
    if(key=='2')
    {
      lcd.setCursor(cursorposition,1);
      lcd.print(number2[counter]);
    }
    else if(key=='3')
    {
      lcd.setCursor(cursorposition,1);
      lcd.print(number3[counter]);
    }
    else if(key=='4')
    {
      lcd.setCursor(cursorposition,1);
      lcd.print(number4[counter]);
    }
    else if(key=='5')
    {
      lcd.setCursor(cursorposition,1);
      lcd.print(number5[counter]);
    }
    else if(key=='6')
    {
      lcd.setCursor(cursorposition,1);
      lcd.print(number6[counter]);
    }
    else if(key=='7')
    {
      lcd.setCursor(cursorposition,1);
      lcd.print(number7[counter]);
    }
    else if(key=='8')
    {
      lcd.setCursor(cursorposition,1);
      lcd.print(number8[counter]);
    }
    else if(key=='9')
    {
      lcd.setCursor(cursorposition,1);
      lcd.print(number9[counter]);
    }
    else if(key=='0')
    {
      lcd.setCursor(cursorposition,1);
      lcd.print(number0[counter]);
    }
    else if(key=='#')
    {
      cursorposition--;
      lcd.setCursor(cursorposition,1);
    }
    else if(key=='*')
    {
      startdone = true;
      lcd.clear();
    }
    prevkey = key ;
  }
  else
  {
    delay(1);
  }
}

How many elements are in this array?

int output = {};

.

Sorry, forgot to mention it, the variable 'value' that you can see there is for our potentiometer, so the input on the array 'output' will depend on the value on the potentiometer.

This is how you set aside space in an array:
int myInts[6];
int myPins = {2, 4, 8, 3, 6};
int mySensVals[6] = {2, 4, -8, 3, 2};
char message[6] = "hello";

BTW, if you are only storing 1-10 in the array, use byte.
int output = {};
. . .
output[a] = random(1,10);

.

Got it. So, the large value is already fixed by changing the 'int' to 'byte', thanks for the help. I also forgot to mention that i'm only planning to have a random integer from 1-9 based from the project that we are making, so I made the random function 'output[a] = random(1,9);', but now sometimes it gives a value of 10 or 11, and the syntax that I did to check if there's a duplication on the output array is not working anymore.

Yes, you need to allocate space for the maximum number of elements you expect in that array.

If you expect 12 elements then you would need:

int output[12];
Or
byte output[12];
.

Wow! Works like a charm @larryd, thanks for the help! I never thought someone will be spending their time helping a student like me, haha. Cheers!

P.S. This isn't the final set of code though, we still need to consider the micro switch and other stuff that is not yet included in our project. But then again, now it works, we can continue building our project. I hope that we won't encounter any problems, but if we managed to encounter one, I pray that someone like you will help us again. Thanks!