How to generate a random color code in C++

In this tutorial, we will learn how to generate a random color code in C++ for RGB and HEX values. We can do this by generating a random number using the rand() function and using it to generate a new number using the modulus operator which is in the required range. Let’s understand how it can be done.

  1. Generate a random RGB color code in C++
  2. Generate a random HEX color code in C++

How to generate a random RGB color code in C++

An RGB color code is a set of three numbers lying between 0 to 255. These numbers represent the extent of Red, Green, and Blue colors respectively. To generate a random RGB color code, we need to do the following.

  • create an array of integers where we can store random values of RGB.
  • call srand() function with argument ‘time(0)’ to make sure the system generates a unique random number every time rand() is called.
  • create a function rgb_color_code() that stores random integers between 0 to 255 in the array created in the first step. This is done using the rand() function and modulus operator. Call this function and then print the array.

See the example code below.

#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
 
void rgb_color_code(int rgb[]);

void rgb_color_code(int rgb[])
{
  int i;
  for(i=0;i<3;i++)
  {
    rgb[i]=rand()%256;
  }
}

int main()
{
  int rgb[3];
  char hex[6];
    srand(time(0));
    
    //RGB color code
    rgb_color_code(rgb);
  cout<<"Random RGB color code: ";
  cout<<"rgb(";
  for(int i=0;i<3;i++)
  {
    cout<<rgb[i];
    //to seperate red, blue, color values by a comma( no comma for last value)
    if(i!=2)
    {
      cout<<", ";
    }
  }
  cout<<")"<<endl;
  
  return 0;
}

 

Output:

Random RGB color code: rgb(36, 97, 218)

We can run this program again to generate more random RGB color codes.

How to generate a random HEX color code in C++

A HEX color code is represented as #RRGGBB. In a HEX color representation, we specify the extent of Red, Green and Blue colors using a 2-digit hexadecimal number respectively. To generate a random hexadecimal number we need to follow the below-given steps.

  • Create a character array to store a hexadecimal string that contains values for red, green and blue color respectively.
  • call srand() function with argument ‘time(0)’ to make sure the system generates a unique random number every time rand() is called.
  • Create a user-defined function hex_color_code() that stores hexadecimal characters in the array created in the first step. We can accomplish this using the rand() function and modulus operator. Call this function in the main() function and print the hexadecimal string.

Here is the example program for the HEX color code.

#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
 
void hex_color_code(char hex[]);

void hex_color_code(char hex[])
{
  char hex_char[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  int i;
  for(i=0;i<6;i++)
  {
    hex[i]=hex_char[rand()%16];
  }
}
 
int main()
{
  int rgb[3];
  char hex[6];
    srand(time(0));
   
  //HEX color code
  
  hex_color_code(hex);
  cout<<"Random HEX color code: ";
  cout<<"#";
  for(int i=0;i<6;i++)
  {
    cout<<hex[i];
  }
    
  return 0;
}

Output:

Random HEX color code: #F27E52

We can generate more HEX color codes by executing the above program again.

Thank you.

Also read: How to generate random hexadecimal strings in C++

Leave a Reply

Your email address will not be published. Required fields are marked *