Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,844 questions

55,671 answers

573 users

How to generate a random color in HEX format with C++

3 Answers

0 votes
#include <iostream>
#include <cstdlib>
#include <ctime>

void generateRandomHEXColorCode(char hex[]) {
   const char hexChars[] = "0123456789ABCDEF";
   
   for (int i = 0; i < 6; i++) {
       hex[i] = hexChars[rand() % 16]; 
   }
}

int main() {
   char hex[7] = {0}; 
   
   srand(time(0)); 
   
   generateRandomHEXColorCode(hex);
   
   std::cout << "Random HEX Color: #" << hex << "\n";
}


/*
run:

Random HEX Color: #E78C75

*/

 



answered Oct 8, 2025 by avibootz
0 votes
#include <iostream>
#include <random>
#include <iomanip> // For formatting output // setw // setfill

int generateRandomNumber(int min, int max) {
    // Use a random device to seed the random number generator
    std::random_device rd;

    std::mt19937 gen(rd()); 
    std::uniform_int_distribution<> dis(min, max);

    return dis(gen);
}

// Function to generate a random RGB color
void generateRandomRGBColor() {
    // Generate random values for Red, Green, and Blue channels
    int red = generateRandomNumber(0, 255);
    int green = generateRandomNumber(0, 255);
    int blue = generateRandomNumber(0, 255);

    // Print the HEX color code
    std::cout << "HEX Color Code: #" 
              << std::hex << std::setw(2) << std::setfill('0') << red
              << std::setw(2) << std::setfill('0') << green
              << std::setw(2) << std::setfill('0') << blue
              << std::dec << std::endl; // Switch back to decimal formatting
}

int main() {
    generateRandomRGBColor();
}


/*
run:

HEX Color Code: #38ce06

*/

 



answered Oct 8, 2025 by avibootz
edited 2 days ago by avibootz
0 votes
#include <iostream>
#include <iomanip>
#include <sstream>
#include <random>

/*
    Generate a random color in HEX format (#RRGGBB).
    This program demonstrates how numbers and bits are used
    to produce a valid 24‑bit color value.
*/

/**
 * Create a 24‑bit random integer (0x000000–0xFFFFFF).
 * Uses C++'s <random> library for high‑quality randomness.
 */
unsigned int randomColorInt() {
    // 24 bits → values from 0 to 16,777,215 (0xFFFFFF)
    static std::random_device rd;
    static std::mt19937 gen(rd());
    static std::uniform_int_distribution<unsigned int> dist(0, 0xFFFFFF);

    return dist(gen);
}

/**
 * Convert a 24‑bit integer into a hex color string.
 * Uses stringstream + std::hex + std::setw + std::setfill
 * to ensure exactly 6 hex digits.
 */
std::string intToHexColor(unsigned int value) {
    std::stringstream ss;

    ss << "#"                      // Leading '#'
       << std::hex                 // Hexadecimal output
       << std::setw(6)             // Always 6 hex digits
       << std::setfill('0')        // Pad with zeros
       << value;

    return ss.str();
}

/**
 * Produce a random hex color by combining the two functions.
 */
std::string generateRandomHexColor(unsigned int &valueOut) {
    valueOut = randomColorInt();       // 24‑bit random number
    
    return intToHexColor(valueOut);    // Convert to #RRGGBB
}

int main() {
    unsigned int value;
    std::string hexColor = generateRandomHexColor(value);

    std::cout << "Random 24‑bit value: " << value << "\n";
    std::cout << "Hex color: " << hexColor << "\n";
}


/*
run:

Random 24‑bit value: 3832388
Hex color: #3a7a44

*/

 



answered 2 days ago by avibootz
...