To generate random number in range [A .. B], first take difference between numbers: D = B-A, then generate a random number in the range of [0..D] and add that to A:
unsigned myRand(unsigned fromA, unsigned toB)
{
if (fromA > toB) // check if fromA is
return myRand(toB, fromA);
int d = toB - fromA; // difference between numbers: D = B-A
int r = rand() % (d+1); // random number in the range of [0..D]
return r + fromA;
}
Or, you may just to it manually like this:
20 + (rand()%11)
Also, note that depending on implementation rand() returns values between 0 and RAND_MAX, so you won't be able to create random values large than RAND_MAX (which might be as small as 32767). In this cases you may combine multiple rand()
calls to get large random numbers:
unsigned r = (rand() << 16) | rand();