How would I randomize colors on LCD?

I have 240x320 LCD with ILI9341 and when I am making something like an old school screensaver with random lines, how can I randomize 8 different defined colors:

#define BLACK   0x0000                // color values
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

using random returns number so how would I make it so:

tft.drawLine(x2, ranyy2, x1, y1, BLUE)

work with random color and not specific defined color name?

Stick the values in an eight element array, then select a random number between 0 and 7, inclusive?

Hi.

I'm sure you know you are sending numbers represented by the words like "BLUE", don't you ? They are just in a different notation. So you can create some number and send it instead of the color names, or convert it to the same notation (which doesn't really make a lot of sense) before you send it . Or of course do as dannable suggests, which would limit the number of colors to only eight, but that was what you seem to be asking for.

Yes I wanted to randomize the fixed set of colors so I don’t end up with 2 very similar color. I ended up expanding to 16 colors and used array. If you got suitable LCD that works, this should be a bit trippy. I am using Adafruit clone 2.8" 240x320 LCD on Uno (should work on other with Uno pinout). Adjust pinout and LCD driver if you need to.

#include <Adafruit_GFX.h>             // let someone else do the work so I don't have to code
#include <Adafruit_TFTLCD.h>          // the whole LCD functions! (hee hee)
#include <MCUFRIEND_kbv.h>       // no seriously thanks them for the hard work making things easy for us!   
MCUFRIEND_kbv tft;
#define LCD_CS A3                     // setting up LCD pinout
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4
int ranx = 120;
int rany = 160;                     // initial number, start first run at center before randomizing
int color1 = 0xF800;                // same with color, red and black at first
int color2 = 0x0000;
int rcol1 = 0;
int rcol2 = 0;
int color[16] = {0x0000, 0x001F, 0xF800, 0x07E0,
                 0x07FF, 0xF81F, 0xFFE0, 0xFFFF,
                 0xFC10, 0x841F, 0xC618, 0x8410,
                 0x4208, 0x87F0, 0x8200, 0xFC00
                };
/*
  #define BLACK   0x0000                       // color values
  #define BLUE    0x001F
  #define RED     0xF800
  #define GREEN   0x07E0
  #define CYAN    0x07FF
  #define MAGENTA 0xF81F
  #define YELLOW  0xFFE0                       // I left these in for fixed color value
  #define WHITE   0xFFFF                       // should someone prefer fixed or to
  #define LTRED   0xFC10                       // change or add to the 16 variable array
  #define LTBLUE  0x841F
  #define LTGRAY  0xC618
  #define GRAY    0x8410
  #define DKGRAY  0x4208
  #define LTGREEN 0x87F0
  #define BROWN   0x8200                       // be sure to remove */  /* to enable these
  #define ORANGE  0xFC00                       // BTW these are the 16 colors of C64 ( I think)
*/

void setup(void) {
  randomSeed(analogRead(5));                 // not using pin A5, use it for random seed
  uint16_t identifier = tft.readID();        // what LCD is installed?
  tft.reset();
  tft.begin(identifier);
}

void loop() {
  //  unsigned long t;
  int           x1, y1, x2, y2,
                w = tft.width(),
                h = tft.height();              // starting variables
  tft.fillScreen(color[0]);                    // clear screen
  x1 = y1 = 0;                                 // resetting variable each loop just in case
  x2 = w - 1;
  y2 = h - 1;

  while (x1 < w)                               // loop until x1 reaches LCD size
  {
    tft.drawLine(x1, 0, ranx, rany, color1);   // draw line from one side to random center
    tft.drawLine(ranx, rany, x1, h, color1);   // draw from center to other side
    x1 ++;
    tft.drawLine(x1, 0, ranx, rany, color2);   // repeat in alternating color
    tft.drawLine(ranx, rany, x1, h, color2);   // draw from center to other side
    x1 ++;
  }
  while (y1 < h)                               // loop until y1 reaches LCD size
  {
    y1 ++;
    tft.drawLine(0, y1, ranx, rany, color1);   // draw line from one side to random center
    tft.drawLine(ranx, rany, w, y1, color1);   // repeat in alternating color
    y1 ++;
    tft.drawLine(0, y1, ranx, rany, color2);   // repeat in alternating color
    tft.drawLine(ranx, rany, w, y1, color2);   // draw from center to other side
  }

  ranx = random(w);                            // after first pass, randomize center!
  rany = random(h);
  rcol1 = random(0,16);                          // random color too!
  rcol2 = random(0,16);
  while (rcol1 == rcol2)                       // 1 in 16 chance it'd be the same color. Re-randomizing until it's not the same
  {
    rcol2 = random(0,16);
  }
  color1 = color[rcol1];
  color2 = color[rcol2];                       // use random color to set color1 and color2 value from array
  delay(2000);                                 // wait a couple seconds to see the result
}