Pickle
Mega GP Mania
In my project I need to generate random numbers. I wanted to generate random numbers between a given range. Searching the web ive come across this solution, but it seems like rand is unstable. I have random [ no pun intended ] lockups in the application, I think this is the code responsible. I commented the srand line first then the rand line. No lock up once the rand line is not used.
Is there a undefined condition for modulus, like a divide by zero condition?
CODE
int16_t RandomValue( int16_t low, int16_t high )
{
int16_t num = 0;
srand((uint8_t)time(0) * rand());
num = rand() % (high - low + 1) + low;
return num;
}
Update: I think I solved my own issue, n % 0 is undefined (its divide by zero). I need to add some code to verify this is the lockup code.
Is there a undefined condition for modulus, like a divide by zero condition?
CODE
int16_t RandomValue( int16_t low, int16_t high )
{
int16_t num = 0;
srand((uint8_t)time(0) * rand());
num = rand() % (high - low + 1) + low;
return num;
}
Update: I think I solved my own issue, n % 0 is undefined (its divide by zero). I need to add some code to verify this is the lockup code.