As a color has RGB data and all values (red, green and blue) are integers from 0 to 255 (0 and 255 included), you can use the
Random
class to generate three integers between 0 and 255.
Random rand = new Random();
int max = byte.MaxValue + 1;
int r = rand.Next(max);
int g = rand.Next(max);
int b = rand.Next(max);
Color c = Color.FromArgb(r, g, b);
rand.Next(max);
returns an integer from 0 to 255, but we pass
max
(= 256) as argument because the argument of
rand.Next
is the exclusive upper bound, so if we use 256 here, it will include 255, but not 256.
More information about
Random.Next
:
http://msdn.microsoft.com/en-us/library/System.Random.Next.aspx[
^]