Which of the following C++ statements displays a random number in the range 1 through 10?

a)
cout << 1 + rand() % 10;

b)
cout << 1 + rand() % (10 -1 + 1);

c)
cout << 1 + rand() % 10 -1 + 1;

d)
first and second answers are correct

e)
all of the above

Respuesta :

Answer:

Option(e)

Explanation:

rand() function results into a random number between 0 to a maximum number RAND_MAX which is a constant. To range it from 1, 1 is added and to decide its maximum value it should be modulo with that number. For example - if a number generated is 56 the 56%10 will be 6 which is in the range of 1 to 10. Option(a) is same as Option (b) because 1 is added and then subtracted which is neutral. Similarly, option (c) is also same.

ACCESS MORE