Random number higher than maximum value.

Hello!

I have an issue with my code: I am trying to generate (pseudo)random numbers for my project and have this line in my code to generate the number:

res1=random(1, 59);

But, when I run that I occasionally get numbers higher than the maximum value of 59 (so far I am only getting 78 that is above the random. Not sure if that's an issue with the random function or just a coincidence as I have only gotten the number above the maximum value twice). Is there something I can fix?

The problem should be easy to reproduce.

Please post a complete program that illustrates it

Here you go:

The Code

It doesn't happen every time but if you want to get to the number generation system more easily you can add:

runRandom();

between line 584 and 585.

Oh. I’m sorry I forgot to say that I am running on an LCD Keypad display.
You can find and replace “lcd.print” with “Serial.print”

Please follow forum guidelines and post the MINIMAL code needed to demonstrate the problem, in the message, using code tags.

The line you posted in the OP works as advertised, so you are doing something else wrong.

Sorry, here you go:

int i;
int res1=1;
int res2=1;
int res3=1;
int res4=1;
int res5=1;
int res6=1;
int resspec;
int matched = 0;

#include <LiquidCrystal.h>

// LiquidCrystal lcd(rs, enable, d4, d5, d6, d7) 
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void runRandom() {
  lcd.clear();
  for(i=0; i<10; i++){
    res1=random(1, 59);
    lcd.setCursor(0, 0);
    lcd.print(res1);
    delay(100);
  }
  for(i=0; i<10; i++){
    res2=random(1, 59);
    lcd.setCursor(4, 0);
    lcd.print(res2);
    delay(100);
  }
  while(res2==res1){
    res2=random(1, 59);
    lcd.setCursor(4, 0);
    lcd.print(res2);
    delay(100);
  }
  for(i=0; i<10; i++){
    res3=random(1, 59);
    lcd.setCursor(8, 0);
    lcd.print(res3);
    delay(100);
  }
  while(res3==res1 || res3==res2){
    res3=random(1, 59);
    lcd.setCursor(4, 0);
    lcd.print(res3);
    delay(100);
  }
  for(i=0; i<10; i++){
    res4=random(1, 59);
    lcd.setCursor(0, 1);
    lcd.print(res4);
    delay(100);
  }
  while(res4==res1 || res4==res2 || res4==res3){
    res4=random(1, 59);
    lcd.setCursor(4, 0);
    lcd.print(res4);
    delay(100);
  }
  for(i=0; i<10; i++){
    res5=random(1, 59);
    lcd.setCursor(4, 1);
    lcd.print(res5);
    delay(100);
  }
  while(res5==res1 || res5==res2 || res5==res3 || res5==res4){
    res5=random(1, 59);
    lcd.setCursor(4, 0);
    lcd.print(res5);
    delay(100);
  }
  for(i=0; i<10; i++){
    res6=random(1, 59);
    lcd.setCursor(8, 1);
    lcd.print(res6);
    delay(100);
  }
  while(res6==res1 || res6==res2 || res6==res3 || res6==res4 || res6==res5){
    res6=random(1, 59);
    lcd.setCursor(4, 0);
    lcd.print(res6);
    delay(100);
  }
  exit(-1);
}

void setup() {
  randomSeed(analogRead(A1));
  lcd.begin(16, 2);
  lcd.clear();
  runRandom();
}

The problem lies in your misuse of the LCD screen. Unless you erase a line or the entire screen, characters will remain from the previous print.

Printing 29, followed by printing 7 on the same line, will look like 79.

  for(i=0; i<10; i++){
    res6=random(1, 59);
    lcd.setCursor(8, 1);
    lcd.print(res6);
    delay(100);
  }

Let me guess, it never happens for res1? :wink:

Hint: what happens when you fist print 124 and after that print 84 in the same location?

And a hint, if you start numbering variables, use an array. That would probably reduce the code of runRandom() to around 20 lines total :wink:

@jremington, damnit, to slow. Spoilers! :stuck_out_tongue:

Thank you! That is the problem.

The constrain function might also be useful

@LandonW, I don't see a use for it here....

ThatRedMelon:
Thank you! That is the problem.

And next week you will be helping someone with the same problem!

Paul