Explanation : An example will help you understand. Let a be 6 and b be 10. There are 5 numbers between 6 to 10 (both inclusive). This is b-a+ 1 (10 - 6 + 1). rand( ) % (b - a + 1) returns an integer from 0 to (b - a). To make it a to b, add a, giving rand() % (b-a+ 1) + a.