Well the function randBetween will return a random integer between 'low' and 'high'. For example if you called it like this:
Code:
int x = randBetween(1, 6);
x would equal a random number between 1 and 6.
So.
Code:
if (low == high) return low;
This is just saying, if you input the same number twice just return that number.
Code:
if (low > high)
{
int tmp = low;
low = high;
high = tmp;
}
This code simply swaps around the low and the high values if they were in the wrong order, eg, if low was 6 and high was 1, it would swap them.
Code:
static bool firstTime = true;
if (firstTime)
{
srand((unsigned int)time(NULL));
firstTime = false;
}
This generates the 'seed' which the function rand() requires. It uses the current time so set the seed. Each time rand() is called subsequently, the seed is changed to the next one in the sequence. Look up random number generators if this doesn't make sense.
Code:
int num = rand();
int result = num % (high - low + 1) + low;
return result;
This generates the random number and returns it. 'num' gets set to a completely random integer. Then you take the modulus of 'num' and (high - low + 1) to get a number between zero and the difference high - low. Then add 'low' and you have a number between low and high.
eg
and num gets set to 12753 by rand().
(high - low + 1) = 6.
num % 6 = 3 (check with a calculator)
3 + low = 4 and that is the number that gets returned.