Hi! Ok so I am having a problem with my code. It's requirements are to put out 100 random number (but these numbers need to be between 1 & 20). My code works in that sense(of printing out between 1 & 20), but it prints out over 100 numbers/times. I am not sure, what exactly I am doing wrong! If someone could help that would be great! Thanks. :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <ctime>
usingnamespace std;
int main()
{
int i = 1;
while (i <= 100)
{
i = (rand() % 20) + 1;
cout << i << " ";
i++;
}
return 0;
}
The problem you have is that you are setting i to a random number between 1-20 so it will never be 100. It will always be below 100 creating a infinite loop. You have to create another variable to hold the random number or change your sentinel value to something else that is not i. Hope that helps. Or makes sense.