How to generate a random HEX RGB color code in C#

1 Answer

0 votes
using System;

class Program
{
    static string GenerateRandomHexColor() {
        var hexChars = "0123456789ABCDEF";
        var rand = new Random();
        var hex = "";
        
        for (int i = 0; i < 6; i++) {
            hex += hexChars[rand.Next(16)];
        }
        
        return hex;
    }

    static void Main()
    {
        Console.WriteLine($"Random HEX Color: #{GenerateRandomHexColor()}"); 
    }
}



/*
run:

Random HEX Color: #AB979E

*/

 



answered Oct 9, 2025 by avibootz
...