The ultimate cause of the problem is that you mix unsigned integers in code without taking the necessary precautions (and without the need).
In particular, if minY sometimes less than 13, a negative iRandomHeight will occur from time to time. What you get is similar to the effect shown below:
#include <limits>
This first generates an unsigned integer, slightly larger than fits into the signed one. mt19937 returns unsigned and sometimes gives values ββlike i in the above code.
The code output above (see liveworkspace ) is as follows:
2 2147484647 -2147482649
The second line shows the result modulo with a negative number (for example, iRandomHeight will ever be), applied to an unsigned integer more than it matches the corresponding signed one. The third line shows what happens when you convert this back to a signed integer (which you implicitly execute when you assign it to one of your signed integer variables).
Although I agree with Haatschii that you should use std::uniform_int_distribution to make your life easier, it is also important to use signed and unsigned values ββeven then.
source share