Well, problem was the way in which I was generating random numbers. Look at the following snippet.
Random randomGen = new Random(System.currentTimeMillis());
Looks very simple and straightforward. But this line was part of a function. Which was structured as
fun()
{
for 1 to 1000
doRandom()
}
...
doRandom()
{
Random randomGen = new Random(System.currentTimeMillis());
number = randomGen.nextInt();
...
}
Has anyone reading it, figured out the problem with above code and why it will work fine while debugging?
If you have. Kudos.
The problem here is seed I am using i.e. System.currentTimeMillis().
- This seed will remain same for next 1/1000th of a second
- That's lots of time for a computer
- While debugging, every break point I place will result in delay and hence will lead to generation of different seed
private Random _randomGen = new Random(System.currentTimeMillis());
...
doRandom()
{
number = _randomGen.nextInt();
...
}
2 comments:
This is a Heisen-bug
It is remarkable, this very valuable message
Post a Comment