[ODE] dRandInt Crash Fix
Colin Bonstead
colin at cyan.com
Tue Jun 21 14:04:34 MST 2005
I recently fixed a crash in our game that was occurring in the ODE step.
The crash typically only happened after the game had been sitting around
running for 8 hours or so, but a few times it happened in less time than
that. It would blow up soon after the joints array was randomized
during the step. It turns out that somehow dRandInt was returning a
number one greater than n, which caused the joint randomization to put
an invalid pointer in the joint array.
I'm not sure how that could happen, but at this point I don't really
have time to investigate it any further, just getting it fixed is more
important. Frankly, I'm not quite sure why dRandInt even needs to use
doubles. Wouldn't just doing dRand() % n give you a number between 0
and n? Regardless, here's a fixed version of dRandInt:
int dRandInt (int n)
{
double a = double(n) / 4294967296.0;
int ret = (int) (double(dRand()) * a);
if (ret >= n)
ret = n-1;
return ret;
}
More information about the ODE
mailing list