hi all. i have a two-dimensional array of System.Drawing.Color that I wish to populate with random colors. I have created a class called Utilities that I want to place a RandomColor() method so that various classes within my program can use it...

so this is what the utilities class looks like so far:

class Utilities
    {
        public static Color RandomColor()
        {
            Color color = new Color();
            
            Random rand = new Random();
            switch (rand.Next(1, 8))
            {
                case 1:
                    color = Color.Blue;
                    break;
                case 2:
                    color = Color.Red;
                    break;
                case 3:
                    color = Color.Green;
                    break;
                case 4:
                    color = Color.Yellow;
                    break;
                case 5:
                    color = Color.Orange;
                    break;
                case 6:
                    color = Color.Violet;
                    break;
                case 7:
                    color = Color.White;
                    break;
                case 8:
                    color = Color.Black;
                    break;
            }
            return color;
        }
    }

and here is the loop where i populate the grid with colors:

public void Update()
        {
            for (int i = 0; i < Width; i++)
                for (int j = 0; j < Height; j++)
                    _grid[i, j] = Utilities.RandomColor();
        }

but the grid is being filled completely with one color. why should this happen? each call to the RandomColor method initializes a new Random number, so why are they all filling with the same color? when I call Update() later in the program, the grid once again fills with the same color, except this time a different one (so it's working but not the way I want it to!)

can anyone tell me what I am doing wrong?

thank you!

-SelArom

this is an odd error yes. because when i debug your code it works as desired. but the moment i remove the breakpoint and allow it to run we have issues again!

but i was able to create a quick fix for you!

create the random number on the Update method and then simply pass the output to the RandomColor on each itteration.

I cant give you a clear answer as to why your first method does not work but i believe it has something to do with the scope in which it is being called.

another solution is to create a private Random variable outside of the method

Class Utilities
{
  private Random rand;
  public Utilities() {
    rand = new Random();
  }
  public Color RandomColor() {
    switch(this.rand.Next(1 , 8);
    //rest of your method
  }
}

as this works just as well and keeps the random inside the class.

i think problem is with

rand= new Random();

which is seed by current time, if you call this too fast, rand will be seed by the same seed value.
you need to create random object once, and continue using it, just like Killer_Typo's solution

actually what happened was that I needed to initialize the rand variable in a STATIC constructor for the class, since i am NOT instantiating it, but rather calling a static method. so I did a

static public Utilities()
{
rand = new Random();
}

that did the trick!

but thanks for your replies!

-SelArom

Oh thx! I have the same problem, my random return always the same number. But i just put my random variable static and all work nice! Was working on this problem for many hours! Thx you again! :D

Just a FYI: You should only ever execute new Random() one time in your code. There is no need to have multiple generators and you can run into timing issues (like the problem here) if you do.

This article has been dead for over six months. Start a new discussion instead.