Alright, so I want to call
for (int i = 0; i < b; b--)
{
color += ChooseColor() + " ";
}
the important part is that it gets called several times. My ChooseColors() is
private static string ChooseColor()
{
Random random = new Random();
var colors = new List<string> { "Blue", "Red", "Green", "Indigo", "Black", "White", "Violet", "Turquoise", "Pink", "Lavender", "Cinder", "Fuschia", "Orange" };
int index = random.Next(colors.Count);
string colorName = colors[index];
colors.RemoveAt(index);
return colorName;
}
the issue is, I want it to call a new instance of ChooseColor everytime. For instance, it would print Black White instead of Black Black. Right now it prints the exact same thing over and over instead of dumping current and calling again (which is what I thought loops did >.<) any suggestions?
The array is declared within the method, so it is redeclared and re-filled when you call the method again. You’d have to declare the list of colors statically outside the method.
Also, the randomizer is initialized again on every method call – initialize the randomizer just once outside the method, too.
Please note that this method returns an empty string now when called with no colors left in the list of colors. I think in addition to fixing the randomizer issue, you should really re-think the design (especially why it should be necessary to remove the colors from the list).