using System;
/*
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.
*/
class Program
{
/// <summary>
/// Create a random 8‑bit integer (0–255).
/// Uses C#'s Random class for efficient integer generation.
/// </summary>
static int RandomChannel(Random rng)
{
// 8 bits → values from 0 to 255
return rng.Next(0, 256); // 256 = 2^8
}
/// <summary>
/// Produce a random RGB color by combining the channels.
/// </summary>
static (int R, int G, int B, string RGB) GenerateRandomRGB(Random rng)
{
int r = RandomChannel(rng); // Red channel (8 bits)
int g = RandomChannel(rng); // Green channel (8 bits)
int b = RandomChannel(rng); // Blue channel (8 bits)
// Construct the CSS-style RGB string
string rgb = $"rgb({r}, {g}, {b})";
return (r, g, b, rgb);
}
static void Main()
{
var rng = new Random();
var result = GenerateRandomRGB(rng);
Console.WriteLine("Red (8 bits): " + result.R);
Console.WriteLine("Green (8 bits): " + result.G);
Console.WriteLine("Blue (8 bits): " + result.B);
Console.WriteLine("RGB color: " + result.RGB);
}
}
/*
run:
Red (8 bits): 71
Green (8 bits): 61
Blue (8 bits): 234
RGB color: rgb(71, 61, 234)
*/