Functions Q.11

0. 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

  • Option : 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.
Cancel reply

Your email address will not be published. Required fields are marked *


Cancel reply

Your email address will not be published. Required fields are marked *