#include <iostream>
#include <random>
/*
Generate a random color in RGB format: rgb(R, G, B)
This program demonstrates how numbers and bits are used
to produce valid 8‑bit channel values.
*/
/**
* Create a random 8‑bit integer (0–255).
* Uses C++'s <random> library for high‑quality randomness.
*/
unsigned int randomChannel() {
// 8 bits → values from 0 to 255
static std::random_device rd;
static std::mt19937 gen(rd());
static std::uniform_int_distribution<unsigned int> dist(0, 255);
return dist(gen);
}
/**
* Produce a random RGB color by combining the channels.
*/
std::string generateRandomRGB(unsigned int &rOut,
unsigned int &gOut,
unsigned int &bOut) {
rOut = randomChannel(); // Red channel (8 bits)
gOut = randomChannel(); // Green channel (8 bits)
bOut = randomChannel(); // Blue channel (8 bits)
// Construct the CSS-style RGB string
return "rgb(" + std::to_string(rOut) + ", "
+ std::to_string(gOut) + ", "
+ std::to_string(bOut) + ")";
}
int main() {
unsigned int r, g, b;
std::string rgb = generateRandomRGB(r, g, b);
std::cout << "Red (8 bits): " << r << "\n";
std::cout << "Green (8 bits): " << g << "\n";
std::cout << "Blue (8 bits): " << b << "\n";
std::cout << "RGB color: " << rgb << "\n";
}
/*
run:
Red (8 bits): 193
Green (8 bits): 106
Blue (8 bits): 110
RGB color: rgb(193, 106, 110)
*/