How to get a random Color in C# and VB.NET

To get a random Color in C# and VB.NET you can use the following snippet.

Sample C#

public static System.Drawing.Color RandomColor()
{
	var rand = new Random();
	var r = rand.Next(0, 256);
	Thread.Sleep(1);
	var g = rand.Next(0, 256);
	Thread.Sleep(1);
	var b = rand.Next(0, 256);
	return System.Drawing.Color.FromArgb(255, r, g, b);
}

Sample VB.NET

Public Shared Function RandomColor() As System.Drawing.Color
	Dim rand As var = New Random
	Dim r As var = rand.Next(0, 256)
	Thread.Sleep(1)
	Dim g As var = rand.Next(0, 256)
	Thread.Sleep(1)
	Dim b As var = rand.Next(0, 256)
	Return System.Drawing.Color.FromArgb(255, r, g, b)
End Function

Please Share us:
Please rate this snippet

One thought on “How to get a random Color in C# and VB.NET

Leave a Reply