Keypad Random Int Generator

Hi guys! I am working on a project which involves using a keypad and having a random int generator. The thing is, occasionally while messing around with the code, like the one I will show, I will only get one int. The int works, but it won’t change. I keep getting the code 364. Please help! Also, I am not the most experienced coder. I am self-taught, so please go easy on me. I am still working on the code right now as well. Here it is:

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


const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};
byte rowPins[ROWS] = {A0, A1, A2, A3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {A4, A5, 2}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

// 16x2 LCD
#define rs 10
#define en 9
#define d4 6
#define d5 5
#define d6 4
#define d7 3
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

String password;
String mypassword;

int redled = 12;
int lock = 13;

int counter = 0;
int attempts = 0;
int max_attempts = 3;

void setup() {
  Serial.begin(9600);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

  pinMode(redled, OUTPUT);
  pinMode(lock, OUTPUT);

  digitalWrite(redled, LOW);
  digitalWrite(lock, LOW);

  Serial.println("enter password");
  lcd.print("Enter Password:");
}

void randpass() {
  password = String(random("1000", "9999"));
}

void loop()
{

  if (Serial.read() == '1') {
    Serial.println(password);
    delay(1000);
    Serial.println(password);
  }


  randpass();
  keypadfunction();

}

void keypadfunction()
{
  char key = keypad.getKey();

  if (key) {
    Serial.println(key);
    counter = counter + 1;
    lcd.setCursor(counter, 1);
    lcd.print("*");
  }
  if (key == '1')
  {

    mypassword = mypassword + 1;
  }

  if (key == '2')
  {

    mypassword = mypassword + 2;
  }

  if (key == '3')
  {

    mypassword = mypassword + 3;
  }

  if (key == '4')
  {

    mypassword = mypassword + 4;
  }

  if (key == '5')
  {

    mypassword = mypassword + 5;
  }

  if (key == '6')
  {

    mypassword = mypassword + 6;
  }

  if (key == '7')
  {

    mypassword = mypassword + 7;
  }

  if (key == '8')
  {

    mypassword = mypassword + 8;
  }

  if (key == '9')
  {

    mypassword = mypassword + 9;
  }

  if (key == '0')
  {

    mypassword = mypassword + 0;
  }


  if (key == '*')
  {
    Serial.println(mypassword);

    if ( password == mypassword )
    {
      lcd.clear();
      lcd.println("Welcome To");
      lcd.setCursor(0, 1);
      lcd.println("ElectroniClinic");
      digitalWrite(lock, HIGH);
      delay(5000);
      digitalWrite(lock, LOW);
      mypassword = "";
      counter = 0;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.println("Enter password");
    }
    else
    {
      Serial.println("wrong");
      digitalWrite(lock, LOW);
      attempts = attempts + 1;
      if (attempts >= max_attempts )
      {
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Locked Out");

        digitalWrite(redled, HIGH);
        delay(5000);
        digitalWrite(redled, LOW);
        attempts = 0;

      }
      mypassword = "";
      counter = 0;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Wrong Password");
      delay(1000);

      lcd.setCursor(0, 1);
      lcd.print("max attempts 3");
      delay(1000);

      lcd.clear();
      lcd.println("Enter password");
      lcd.setCursor(0, 1);
    }

  }


}

Since when does random() accept character arrays as parameters?

  password = String(random("1000", "9999"));

I had been trying to use integers

random(1000, 9999)
but it wouldn't work. I was just grasping for straws at that point. Any suggestions?

but it wouldn't work.

That's not helpful.

void randpass() {
  password = String(random("1000", "9999"));
}

I had been trying to use integers

random(1000, 9999)
but it wouldn't work. I was just grasping for straws at that point. Any suggestions?

Random

Random neither accepts or returns Strings, it accepts and returns integers.

I think you are confused between variables, which means numbers, and strings, which means text. I suggest you read about variable types until you are sure you understand the difference. There is information on this site, plus I found this C Tutorial very helpful when I was learning C.

I think you think there is a quick fix for this, there isn't. The problem comes from you trying to dive into the deep end when you are only just getting used to being in the paddling pool, or at least that's how it looks to me. I've not read back over your previous posts to get a feel for what you know but please go back and learn the basics, there's lots of information and tutorials on here and elsewhere. Do the tutorials until it is obvious why a string was never going to work with random and you feel daft for thinking it would. Then try your project again.

It would help you if you broke the project into smaller parts, so, as you are having problems with random make a small program that gets random numbers using random and sends them to the serial monitor. Play around with it and see what works and what doesn't. When you understand it move on to another part of your project and make a small program to test that. When all the separate bits work and you understand them put them together.

Have fun learning.

A plausible way to get a random number - or seed - is to read micros() when a key is actually pressed, discard at least the four least significant bits and only use as many remaining least significant bits as necessary .

When I say I am a beginner, I mean in this area. I am quite experienced and am coding for companies and I am currently creating a system for my own right now. As I said, I don't have much experience in random integers and I was just grasping at straws. I have been trying to make other pieces of code where I set the String variable equal to String(random(1000,9999)), but it just wasn't working. Please help. Also, please don't act like I'm a baby. That is one thing I have found in this forum. It's almost like you guys enjoy degrading others that have less experience than you. Thanks for any useful help though.

Also, please don't act like I'm a baby.

I don't think any of us has. I think we have given the best response we can based on what we know about you, which is not much.

cbgfgfgffqawesrdthfgkyjjdtfrhkyulihygt6uiyo87y6t5rufv6tgyik76t5r4eyrft76f5rdecrtfygu87yb8uijokpl[;';?o.iklo;p' [poio;p' ][poopkoiuygkil o okuli;mo8jhti6gry6ut7ly8itkuvryvutiyturyui

This is the sort of thing I did when I was 10 years old....

Based on the information you've given and my own level of (in)expertise I can't think of anything else to help you, sorry.

Sorry for being abrupt. I have just been working for like a full straight 12 hours trying to troubleshoot and I have gone downhill. Haha! Sorry again!

Again, sorry for my short temper. I am just very tired and frustrated. Thanks again for all of the good advice.

Cessna172:
Sorry for being abrupt. I have just been working for like a full straight 12 hours trying to troubleshoot and I have gone downhill. Haha! Sorry again!

In my experience when I have been working for hours and hours on something and cannot get it to work then what seemed like an impossible problem when I was tired becomes really easy after some sleep.

Thanks. I have and I think I have a new approach. Thanks again for all the help!