I am currently coding a roguelike, and naturally am using a lot of random number generation.
The problem I'm running up on is that if I "overheat" rand(); my program will crash.
If i'm only generating 20 or so ints per frame, it's fine... but when the amount of random numbers goes into the hundreds, the program crashes. The more I'm producing every frame, the sooner it crashes... which leads me to believe there is some pileup going on.
I've done tests, and at 20 rand(); calls per frame, it will run for 24 hours straight at max speed without crashing. Triple that and it doesn't make it ten minutes.
If I put srand(); in the initialization, i can churn out thousands of random numbers before it locks up - but if I put srand(); within the frame itself, i make it about 2-8 frames. If it matters, I'm using time(null) to seed.
the more frequently i call rand(); the sooner it crashes.
Help?
A few comments and ideas on how to narrow down the source of the issue:
srand()
or rand()
functions causing the crash/lock up. Chances are that one, or more, combinations of random numbers is getting your engine into a state where something bad happens.srand(NULL)
try using a constant seed like srand(12345)
. Depending on what other factors your engine uses (like user input) this may be enough to get it to crash in the same spot each time.#ifdef
s, commenting out code, setting application options, or even creating a temporary copy of the project so you can simply delete code, compile, and test. This may be difficult if the project is large/complex.