C# Using random Function
-
code for window form application to change background color using random function anser please Regards Pintu Kumar
Easy enough, it you just generate a random number for Red, Green and Blue values, then feed those to Color.From(int, int, int)[^] to get a Color back from it. You should know what to do with that I think. No, we're not writing your code for you.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
code for window form application to change background color using random function anser please Regards Pintu Kumar
-
code for window form application to change background color using random function anser please Regards Pintu Kumar
Generating a random colour is easy,
Color.FromArgb(random.Next() | ~0x00FFFFFF)
~0x00FFFFFF is a trick to write unchecked((int)0xFF000000), and the bitwise OR with that value sets the Alpha component to 255 (ie, fully opaque). It's the "most random" way to generate a colour, that is, each of the 16777216 opaque colours is generated with equal probability. Unfortunately, most of those colours look like poo. The solution is to generate a random colour in HSV space,
Color.FromArgb(HSV2RGB(random.NextDouble(), 1, 1))
// 1, 1 makes saturated, bright coloursstatic int HSV2RGB(double h, double s, double v)
{
int h_i = (int)(h * 6);
double f = h * 6 - h_i;
double p = v * (1 - s);
double q = v * (1 - f * s);
double t = v * (1 - (1 - f) * s);
double r, g, b;
switch (h_i)
{
default:
r = v; g = t; b = p;
break;
case 1:
r = q; g = v; b = p;
break;
case 2:
r = p; g = v; b = t;
break;
case 3:
r = p; g = q; b = v;
break;
case 4:
r = t; g = p; b = v;
break;
case 5:
r = v; g = p; b = q;
break;
}
return (-0x01000000 | ((int)(255 * r) << 16)) | (((int)(255 * g) << 8) | (int)(255 * b));
}-0x01000000 is yet an other trick to write unchecked((int)0xFF000000). When generating multiple colours, this method still sort of fails: it will produce clashing colours. To avoid that, start with a random colour and add a specific offset
double h_colour = random.NextDouble();
for (int i = 0; i < colours.Length; i++)
{
colours[i] = Color.FromArgb(HSV2RGB(h_colour, 1, 1));
h_colour = (h_colour + 0.618033988749895) % 1; // that's the golden ratio
} -
code for window form application to change background color using random function anser please Regards Pintu Kumar
Sounds like homework to me but here's a couple of clues:
// Instantiate a Random number generator
Random myRandom = new Random();// Get a random number between 0 and 100
int randomNumber = myRandom.Next(101);Now, how could I take my randomNumber and select a colour based on that value..? Perhaps you could do a little research into system.drawing.color? Then I need to do something like the code below to change the background colour... this.BackColor = System.Drawing.Color.Red;
-
Generating a random colour is easy,
Color.FromArgb(random.Next() | ~0x00FFFFFF)
~0x00FFFFFF is a trick to write unchecked((int)0xFF000000), and the bitwise OR with that value sets the Alpha component to 255 (ie, fully opaque). It's the "most random" way to generate a colour, that is, each of the 16777216 opaque colours is generated with equal probability. Unfortunately, most of those colours look like poo. The solution is to generate a random colour in HSV space,
Color.FromArgb(HSV2RGB(random.NextDouble(), 1, 1))
// 1, 1 makes saturated, bright coloursstatic int HSV2RGB(double h, double s, double v)
{
int h_i = (int)(h * 6);
double f = h * 6 - h_i;
double p = v * (1 - s);
double q = v * (1 - f * s);
double t = v * (1 - (1 - f) * s);
double r, g, b;
switch (h_i)
{
default:
r = v; g = t; b = p;
break;
case 1:
r = q; g = v; b = p;
break;
case 2:
r = p; g = v; b = t;
break;
case 3:
r = p; g = q; b = v;
break;
case 4:
r = t; g = p; b = v;
break;
case 5:
r = v; g = p; b = q;
break;
}
return (-0x01000000 | ((int)(255 * r) << 16)) | (((int)(255 * g) << 8) | (int)(255 * b));
}-0x01000000 is yet an other trick to write unchecked((int)0xFF000000). When generating multiple colours, this method still sort of fails: it will produce clashing colours. To avoid that, start with a random colour and add a specific offset
double h_colour = random.NextDouble();
for (int i = 0; i < colours.Length; i++)
{
colours[i] = Color.FromArgb(HSV2RGB(h_colour, 1, 1));
h_colour = (h_colour + 0.618033988749895) % 1; // that's the golden ratio
}harold aptroot wrote:
~0x000000FF ~0x00FFFFFF is a trick to write unchecked((int)0xFF000000)
FTFY (~0x000000FF gives 0xFFFFFF00)
-
harold aptroot wrote:
~0x000000FF ~0x00FFFFFF is a trick to write unchecked((int)0xFF000000)
FTFY (~0x000000FF gives 0xFFFFFF00)