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 RGB format in C#

2 Answers

0 votes
using System;

class Program
{
    // Function to generate a random number between min and max (inclusive)
    static int GenerateRandomNumber(int min, int max) {
        Random rand = new Random();

        return rand.Next(min, max + 1);
    }

    // Function to generate a random RGB color
    static void GenerateRandomRGBColor() {
        int red = GenerateRandomNumber(0, 255);
        int green = GenerateRandomNumber(0, 255);
        int blue = GenerateRandomNumber(0, 255);

        Console.WriteLine($"Random RGB Color: rgb({red}, {green}, {blue})");
    }

    static void Main()
    {
        GenerateRandomRGBColor();
    }
}


/*
run:

Random RGB Color: rgb(214, 72, 157)

*/

 



answered Oct 9, 2025 by avibootz
0 votes
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)

*/

 



answered 2 days ago by avibootz
...