I hope someone can get their head round this because it's driving me nuts! I have the following static method in a class called Pie which returns a random hex value:
By itself this works, but if I repeatedly call it I find it keeps returning the same 'random' value (although this value is different on each page load). In other words, if I use the code...
...I'm getting a response of something like this:
Then I refresh the page and get something like this:
Now, here's the really odd thing, if I stick a breakpoint in the method and debug it, the output is correct i.e. something like this:
But as soon as I remove that breakpoint again it resorts back to repeating the same value. Can anyone replicate this behaviour? And if so, what's going on???
Code:
public static string RandomHex()
{
string strValidChars = "0123456789ABCDEF";
string strRandomHex = String.Empty;
Random objRandom = new Random();
for (int i = 0; i < 6; i++)
{
int intRandom = objRandom.Next(0, strValidChars.Length);
strRandomHex += strValidChars[intRandom];
}
return strRandomHex;
}
By itself this works, but if I repeatedly call it I find it keeps returning the same 'random' value (although this value is different on each page load). In other words, if I use the code...
Code:
for (int i = 0; i < 10; i++)
{
Response.Write(Pie.RandomHex() + "<br />");
}
...I'm getting a response of something like this:
Code:
6F6AE0
6F6AE0
6F6AE0
6F6AE0
6F6AE0
6F6AE0
6F6AE0
6F6AE0
6F6AE0
6F6AE0
Then I refresh the page and get something like this:
Code:
2ED772
2ED772
2ED772
2ED772
2ED772
2ED772
2ED772
2ED772
2ED772
2ED772
Now, here's the really odd thing, if I stick a breakpoint in the method and debug it, the output is correct i.e. something like this:
Code:
168903
CCAFCC
6B3751
13C785
663CD4
3F6390
377D7A
8390C1
63F83A
74B11D
But as soon as I remove that breakpoint again it resorts back to repeating the same value. Can anyone replicate this behaviour? And if so, what's going on???