a % b as above will count the number of times b fits into a and return the remainder. As an example 5 % 4 = 1, 2 % 4 = 2, 4 % 4 = 0 and 11 % 4 = 3. The effect of using something like this:rand() % max_numbermax_number > 0, is basically that you will get an integer less than the value of max_number. Conversely the following will limit your number to a defined range:rand() % (max_number - min_number) + min_numbermin_number < max_number, but neither need necessarily be larger than 0.1 / max_number, regardless of the number. You can think of this as a roulette-wheel or dice type of random number.
int rand_number = rand() % ( b-a) +a ; range ( b -a ) to a |
| How can I get random integers in a certain range? A: The obvious way, rand() % N /* POOR */ (which tries to return numbers from 0 to N-1) is poor, because the low-order bits of many random number generators are distressingly non-random. (See question 13.18.) A better method is something like (int)((double)rand() / ((double)RAND_MAX + 1) * N) |