简体   繁体   中英

Changing method call every time loop executes C#

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.

private static Random random = new Random();
private static List<string> colors = new List<string> { ... };

private static string ChooseColor()
{
    if (colors.Count > 0)
    {
        int index = random.Next(colors.Count);
        string colorName = colors[index];
        colors.RemoveAt(index);
        return colorName;
    }

    return String.Empty;
}

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).

In that case creat instance of Random class static.

this way

static Random random = new Random(); // Global Declaration

private static string ChooseColor()
{
    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;
}

For More read this

Generated random numbers are always equal

You are creating Random instances too close in time. As they are seeded from the clock, they will all start at the same number. Create one instance outside the function, and send it aloing whey you call it.

Also, you are creating a new list each time, so removing items from the list has no effect. Create the list outside the function:

Random random = new Random();
List<string> colors = new List<string> { "Blue", "Red", "Green", "Indigo", "Black", "White", "Violet", "Turquoise", "Pink", "Lavender", "Cinder", "Fuschia", "Orange" };
for (int i = 0; i < b; b--) {
  color += ChooseColor(colors, random) + " ";
}

private static string ChooseColor(List<string> colors, Random random) {
  int index = random.Next(colors.Count);
  string colorName = colors[index];
  colors.RemoveAt(index);
  return colorName;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM