I need to get some random colors to draw a pie. My code works but it can take the same color again and again
Random r = new Random();
for (int i = 0; i < 20; i++)
{
int min = 0;
int max = 255;
int rand1 = r.Next(min, max);
int rand2 = r.Next(min, max);
int rand3 = r.Next(min, max);
Color myColor = Color.FromArgb(rand1, rand2, rand3);
//drawing the pie here
}
How can I rework it so that it’s not going to pick the same color again.
You could put the random colors you generate in a container, then check if a similar color by a certain delta has already been inserted in the container.
This way you won’t risk to pick up a different color but very similar, like:
RGB(0, 0, 0)andRGB(10, 2, 3)