Nothing comes out because, the vast majority of the time, rand() will be greater than 999. You could put a while loop around rand() and if statement, but there's an easier way to get a three-digit number. Simply take the modulo (%) of rand() and 1000. Also, you have to use srand(time(NULL)), otherwise, your code will generate the same random value every time. Finally, your variable in this case is r, so you need to use cout << r << endl;
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main()
{
srand(time(NULL));
int r = rand() %1000;
cout << r << endl;
return 0;
}
Thank you guys i got it to work!
@dangerous
I used your code for the eight random non positive integers but the code for
the positive 3 digit integers kept giving me negative so this is what i used
int num;
int count = 1;
unsigned seed = time(0);
srand(seed);
while (count <= 1) {
num = 100 + rand() % 10;
cout << num << endl;
count++;
}