Generate Random Colors in C++ - Hex, RGB & Qt QColor
Complete code tutorial with examples and best practices
[ Code Example - Quick Summary ]
Language: C++
What: Generate random colors in C++ using hex strings, RGB values, and Qt QColor objects with modern C++11 random number generation.
Try it: Use our interactive Colors generator or integrate this code into your C++ application.
Generate random colors in C++ using hex strings, RGB values, and Qt QColor objects with modern C++11 random number generation. Looking for other languages? Check our code examples in PHP , JavaScript , Python , Java , C# , Ruby and Go or use our interactive web generator.
C++ Code Example
#include <iostream>
#include <random>
#include <sstream>
#include <iomanip>
#include <string>
class RandomColorGenerator {
private:
std::random_device rd;
std::mt19937 gen;
std::uniform_int_distribution<> rgb_dist;
std::uniform_int_distribution<> hue_dist;
std::uniform_int_distribution<> percent_dist;
public:
RandomColorGenerator()
: gen(rd()),
rgb_dist(0, 255),
hue_dist(0, 360),
percent_dist(0, 100) {}
std::string randomHexColor() {
std::stringstream ss;
ss << "#"
<< std::hex << std::setfill('0')
<< std::setw(2) << rgb_dist(gen)
<< std::setw(2) << rgb_dist(gen)
<< std::setw(2) << rgb_dist(gen);
return ss.str();
}
std::string randomRgbColor() {
int r = rgb_dist(gen);
int g = rgb_dist(gen);
int b = rgb_dist(gen);
return "rgb(" + std::to_string(r) + ", " +
std::to_string(g) + ", " +
std::to_string(b) + ")";
}
std::string randomHslColor() {
int h = hue_dist(gen);
int s = percent_dist(gen);
int l = percent_dist(gen);
return "hsl(" + std::to_string(h) + ", " +
std::to_string(s) + "%, " +
std::to_string(l) + "%)";
}
std::string randomPastelColor() {
int h = hue_dist(gen);
std::uniform_int_distribution<> pastel_s(25, 100);
std::uniform_int_distribution<> pastel_l(70, 90);
int s = pastel_s(gen);
int l = pastel_l(gen);
return "hsl(" + std::to_string(h) + ", " +
std::to_string(s) + "%, " +
std::to_string(l) + "%)";
}
struct RGB {
int r, g, b;
};
RGB randomRgbStruct() {
return {rgb_dist(gen), rgb_dist(gen), rgb_dist(gen)};
}
};
int main() {
RandomColorGenerator colorGen;
std::cout << "Hex Color: " << colorGen.randomHexColor() << std::endl;
std::cout << "RGB Color: " << colorGen.randomRgbColor() << std::endl;
std::cout << "HSL Color: " << colorGen.randomHslColor() << std::endl;
std::cout << "Pastel: " << colorGen.randomPastelColor() << std::endl;
auto rgb = colorGen.randomRgbStruct();
std::cout << "RGB Struct: (" << rgb.r << ", "
<< rgb.g << ", " << rgb.b << ")" << std::endl;
return 0;
}
[EXPLANATION]
Modern C++ (C++11+) uses std::random_device for seed generation and std::mt19937 (Mersenne Twister) for high-quality random numbers. std::uniform_int_distribution generates uniformly distributed integers. For hex colors, use std::stringstream with std::hex and std::setw(2) for 2-digit hex values. RGB uses three random integers (0-255). Create member distributions in the class for efficiency instead of recreating them. For Qt applications, you can create QColor objects with RGB values.
Expected Output
Hex Color: #6b4e9a RGB Color: rgb(187, 98, 143) HSL Color: hsl(234, 67%, 54%) Pastel: hsl(89, 78%, 85%) RGB Struct: (142, 207, 89)
Common Use Cases
- Generate colors for Qt GUI applications
- Create color palettes for game development
- Assign colors to 3D rendering objects
- Generate test data for graphics libraries
- Create random materials for OpenGL/Vulkan
Important Notes
-
Use
std::mt19937for better quality thanrand() -
std::random_deviceprovides non-deterministic random seeds - Create distributions once and reuse for performance
-
Qt uses
QColor(r, g, b)for RGB colors -
For hex strings,
std::setfill('0')pads with zeros
Try Our Interactive Generator
Don't want to write code? Use our free web-based Colors generator with instant results.
TRY COLORS GENERATOR →Other Programming Languages
View Colors generation code examples in PHP
View Colors generation code examples in JavaScript
View Colors generation code examples in Python
View Colors generation code examples in Java
View Colors generation code examples in C#
View Colors generation code examples in Ruby
View Colors generation code examples in Go