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)
and RGB(10, 2, 3)
const int DELTA_PERCENT = 10;
List<Color> alreadyChoosenColors = new List<Color>();
// initialize the random generator
Random r = new Random();
for (int i = 0; i < 20; i++)
{
bool chooseAnotherColor = true;
while ( chooseAnotherColor )
{
// create a random color by generating three random channels
//
int redColor = r.Next(0, 255);
int greenColor = r.Next(0, 255);
int blueColor = r.Next(0, 255);
Color tmpColor = Color.FromArgb(redColor, greenColor, blueColor);
// check if a similar color has already been created
//
chooseAnotherColor = false;
foreach (Color c in alreadyChoosenColors)
{
int delta = c.R * DELTA_PERCENT / 100;
if ( c.R-delta <= tmpColor.R && tmpColor.R <= c.R+delta )
{
chooseAnotherColor = true;
break;
}
delta = c.G * DELTA_PERCENT / 100;
if ( c.G-delta <= tmpColor.G && tmpColor.G <= c.G+delta )
{
chooseAnotherColor = true;
break;
}
delta = c.B * DELTA_PERCENT / 100;
if ( c.B-delta <= tmpColor.B && tmpColor.B <= c.B+delta )
{
chooseAnotherColor = true;
break;
}
}
}
alreadyChoosenColors.Add(tmpColor);
// you can safely use the tmpColor here
}