I have started this program which is supposed to generate 10 random numbers between 1 and 100 (inclusive). Right now, the program is generating 10 random numbers but they are not within 1 and 100. By the way, I am not allowed to use srand. Any tips?
Thanks!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
usingnamespace std;
/*Write a program which returns 10 random numbers between 1 and 100.
Use the "rand" function and the % operator.
*/
int main ()
{
for (int nCount = 0; nCount < 10; ++nCount) //print 10 numbers
{
cout << rand() << endl;
} while (rand() % 100 +1); //numbers must be between 1 and 100 (inclusive)
}
Values returned by rand in line 15 and in line 17 have no connection. The right way would be cout << rand() % 100 + 1. Also, while loop in your code has no effect on anything. It starts after for loop and has no statements.