Sign in to follow this  
belgreth

Why is this happening?

Recommended Posts

I came across this issue while testing things out..I have a function that just calls a random number 0-9 and returns a Color. I call it during a loop in my program.

To test things, I keep track of what color was returned. Running the program, all colors returned were the same. Odd. So I ran debugger and stopped it for each return and after it was completed, every color was different...Why is running debugger causing my returns to be different?

Here's my silly function:

private Color RandomColor()
{
Random rand = new Random();
int randNum = rand.Next(10);

switch (randNum)
{
case 0:
return Color.Red;
case 1:
return Color.Blue;
case 2:
return Color.Green;
case 3:
return Color.Yellow;
case 4:
return Color.White;
case 5:
return Color.Purple;
case 6:
return Color.Orange;
case 7:
return Color.Brown;
case 8:
return Color.Cyan;
case 9:
return Color.Magenta;
default:
return Color.Black;
}
} // end method RandomColor


Then for example I just have an array that keeps track of the returned values.

for (int x = 0; x < 10; x++)
{
Color randColor;


randColor = RandomColor();

testColors[testColorCount].R = randColor.R;
testColors[testColorCount].G = randColor.G;
testColors[testColorCount].B = randColor.B;

// also tried testColors[testColorCount] = randColor;
testColorCount++;
}

Why are all colors in testColors the same when I just run the program but when debugging it and stopping the execution at each return in the RandomColors function..they're different?

Share this post


Link to post
Share on other sites
Random seeds itself from the current system time, but it has a very limited resolution. So rapidly creating new Random objects will seed it with the same value unless you're making at least a few dozen or more. If you ran this to return a hundred results you'd see the change after some number of rows.

What you need to do is create a Random object and pass that to your random color method instead of creating one in the method itself. That should only be done if you know that method will not be called in quick succession.

Try this.

private Color RandomColor(Random rand)
{
int randNum = rand.Next();

switch (randNum)
{
case 0:
return Color.Red;
case 1:
return Color.Blue;
case 2:
return Color.Green;
case 3:
return Color.Yellow;
case 4:
return Color.White;
case 5:
return Color.Purple;
case 6:
return Color.Orange;
case 7:
return Color.Brown;
case 8:
return Color.Cyan;
case 9:
return Color.Magenta;
default:
return Color.Black;
}
} // end method RandomColor


Then for example I just have an array that keeps track of the returned values.

Random rand = new Random();

for (int x = 0; x < 10; x++)
{
Color randColor;

randColor = RandomColor(rand);

testColors[testColorCount] = randColor;

testColorCount++;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this