This sounds like a rather common problem with Random
. It usually goes away when you make it a class level variable:
public static Random r = new Random();
Usually one needs only one single Random
generator, which calls for making it static.
But why did the MessageBox
help?
The problem most often arises when instead of keeping a single static instance of a Random
you create fresh instances in quick succession. This can happen so fast, that they get the same default seed, derived from current time and so will create the same sequence of numbers.
Now, showing a MessageBox
lets a lot of time pass between the creation of the new Random
instances, so the problem seems to be solved but really is just hidden..
One other even more devious way of hiding this (and other time related problems) it is using the debugger - but don't let that keep you from using it ;-)