I need to generate to generate random colors in Hex values in my asp.net application to draw a graph .
Random random = new Random();
color = String.Format(\"#
I can't see enough of your code, but this should help if I'm guessing right about how the code is structured... it should clarify why the numbers are the same, or close to it, because I had a similar problem and here's how I solved it and why I think this was the solution.
I had some code that generated randoms that looked like this (vastly simplified)
for (some loop logic)
{
Random r = new Random();
int myRandomNumber = Random.Next()
}
When this executed it actually created the exact same numbers for a part of the loop, (say 8 iterations) then switched to a new number repeated for 9 iterations, etc. I solved it by changing it to look like this:
Random r = new Random();
for (some loop logic)
{
int myRandomNumber = Random.Next()
}
I'm sure others will phrase it better, but by declaring the Random outside the loop, the instance of the Random class was able to keep track of the last random number generated, and ensure that the next was actually random. By having it IN the loop (as in the first example) each iteration created a new Random object, so it just used whatever logic (time-based I assume) to generate a random number not knowing that a different instance had just generated the same number.
Boy, I hope that made sense... It's late here, and I'm not explaining clearly, but I think that your issue may be that you're creating a new instance of the random class in your loop logic, and you need to declare it outside the loop.
Edit - added
This article covers why this is the case:
http://geekswithblogs.net/kakaiya/archive/2005/11/27/61273.aspx