Assume that the random number generating function - rand( ), returns an integer between 0 and 10000 (both inclusive). To randomly generate a number between a and b (both inclusive), use the expression
A. | rand ( ) % (b-a) |
B. | (rand ( ) % a) + b |
C. | (rand( ) % (b-a)) + a |
D. | (rand( ) % (b-a+l)) + a |
Answer : D 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. |
|
Option: A Explanation : Explanation will come here. Explanation will come here. Explanation will come here. Explanation will come here. Explanation will come here. |