Random() always returns the same value

you may reckognize a post i did earlier, about variables and how i should have put them outside the setup() code. well, i have another problem. the random function is not working properly. i tested by rebooting the arduino over and over again, and it always returned the same result. i know again some of this is about displays, but it is the random function not working not display stuff.the random() functions are located on lines 48 and 49. here is my code:

#include <LCDWIKI_GUI.h> //Core graphics library
#include <LCDWIKI_KBV.h> //Hardware-specific library



//the definiens of 16bit mode as follow:
//if the IC model is known or the modules is unreadable,you can use this constructed function
LCDWIKI_KBV mylcd(ILI9486,40,38,39,-1,41); //model,cs,cd,wr,rd,reset

//define some colour values
#define BLACK       0x0000
#define BLUE        0x001F
#define RED         0xF800
#define GREEN       0x07E0
#define CYAN        0x07FF
#define MAGENTA     0xF81F
#define YELLOW      0xFFE0
#define WHITE       0xFFFF
#define ORANGE      0xFF8C
#define BROWN       0x79E0
#define LIGHT_GREY  0xBDF7
#define DARK_GREY   0x7BEF
#define GREEN       0x07E0


/*
void generate_spr1_vars() {
  int side = random(2);
  int length = random(50, 170);
  
  if (side == 1) {
    int x1spr1 = mylcd.Get_Display_Width()-length;
    int y1spr1 = mylcd.Get_Display_Height()-10; //-i
    int x2spr1 = mylcd.Get_Display_Width();
    int y2spr1 = mylcd.Get_Display_Height(); //-i
  }
  else {
    int x1spr1 = 0;
    int y1spr1 = mylcd.Get_Display_Height()-10; //-i
    int x2spr1 = length;
    int y2spr2 = mylcd.Get_Display_Height(); //-i
  }
}
*/

void generate_rectangle(int i) {
  mylcd.Set_Draw_color(DARK_GREY);
  int side = random(2);
  int length = random(50, 170);
  
  if (side == 1) {
    mylcd.Fill_Rectangle(mylcd.Get_Display_Width()-length, mylcd.Get_Display_Height()-10-i, mylcd.Get_Display_Width(), mylcd.Get_Display_Height()-i);
  }
  else {
    mylcd.Fill_Rectangle(0, mylcd.Get_Display_Height()-10-i, length, mylcd.Get_Display_Height()-i);
  }
}

//define variables outside of setup so they are GLOBAL
int x1spr1 = 0;
int y1spr1 = 0;
int x2spr1 = 0;
int y2spr1 = 0;


void setup() {
  mylcd.Init_LCD();
  mylcd.Fill_Screen(CYAN);
  
  //set the sprite heights
  int spr1height = 0;
  int spr2height = 0;
  int spr3height = 0;
  int spr4height = 0;
  int spr5height = 0;
  int spr6height = 0;
  
  //get sprites 1 through 6 on the screen
  
  //generate sprite 1 variables
  int side = random(2);
  int length = random(50, 170);
  
  if (side == 1) {
    x1spr1 = mylcd.Get_Display_Width()-length;
    y1spr1 = mylcd.Get_Display_Height()-10; //-i
    x2spr1 = mylcd.Get_Display_Width();
    y2spr1 = mylcd.Get_Display_Height(); //-i
  }
  else {
    x1spr1 = 0;
    y1spr1 = mylcd.Get_Display_Height()-10; //-i
    x2spr1 = length;
    y2spr1 = mylcd.Get_Display_Height(); //-i
  }
  
  //actually generate sprite 1
  mylcd.Set_Draw_color(DARK_GREY);
  mylcd.Fill_Rectangle(x1spr1, y1spr1-spr1height, x2spr1, y2spr1-spr1height);
  
  for(int i = 1; i < 35; ++i) {
    //new frame
    mylcd.Set_Draw_color(CYAN);
    mylcd.Fill_Rectangle(x1spr1, y1spr1-spr1height, x2spr1, y2spr1-spr1height);
    spr1height = ++spr1height;
    spr1height = ++spr1height;
    spr1height = ++spr1height;
    spr1height = ++spr1height;
    spr1height = ++spr1height;
    spr1height = ++spr1height;
    spr1height = ++spr1height;
    mylcd.Set_Draw_color(DARK_GREY);
    mylcd.Fill_Rectangle(x1spr1, y1spr1-spr1height, x2spr1, y2spr1-spr1height);
  }
}

/*void loop() {
  mylcd.Fill_Screen(CYAN);
  
  
  //generate sprite 1 variables
  int side = random(2);
  int length = random(50, 170);
  
  if (side == 1) {
    int x1spr1 = mylcd.Get_Display_Width()-length;
    int y1spr1 = mylcd.Get_Display_Height()-10; //-i
    int x2spr1 = mylcd.Get_Display_Width();
    int y2spr1 = mylcd.Get_Display_Height(); //-i
  }
  else {
    int x1spr1 = 0;
    int y1spr1 = mylcd.Get_Display_Height()-10; //-i
    int x2spr1 = length;
    int y2spr2 = mylcd.Get_Display_Height(); //-i
  }
  
  
  delay(2500);
  
  //move sprites 1 through 6 up
  height1
}*/

void loop() {
  //this is just so i dont get any errors if i dont have a loop in my code! ;)
}

i hope the code clarifies anything about my problem. all answer are appreciated!

A look at the reference for the random() function may shed some light on your problem.

If it is important for a sequence of values generated by random() to differ, on subsequent executions of a sketch, use [url=https://www.arduino.cc/reference/en/language/functions/random-numbers/randomseed/]randomSeed()[/url] to initialize the random number generator with a fairly random input, such as analogRead() on an unconnected pin.

FYI the result for this line of code is undefined:

    spr1height = ++spr1height;

I assume you just want to increment spr1height in which case:

    spr1height++;

or:

    ++spr1height;

Will work.

1 Like

ToddL1962:
FYI the result for this line of code is undefined:

Not just undefined but different versions of the avr-gcc compiler produce different results.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.