I'm trying to make a guessing game program where the user thinks of a number between 1 and 100, and then the computer randomly generates(guesses) a number between 1 and 100. Then the user tells the computer if their number is higher(H), lower(L), or the same(Y) as the guessed number. The problem I am having is changing the range that the computer generates its guess in.
So let's say my number is 43 and the computer generates 15 as its guess. I would input H, indicating that my number is higher than its guess. The computer then changes the parameters to generate a number between 16 and 100.
int high = 100, low = 1;
char answer;
Setting my variables.
srand(time(0));
I have this to truly randomize the generation.
int guess = rand() % high + low;
cout << "Is your number " << guess << "?\n";
cin >> answer;
Generate the number, ask if the number is what I'm thinking of, and then ask me to answer if it is higher, lower, or correct.
Then the segment that checks the answer and determines how to change the range of the randomly generated number.
switch (answer)
{
case 'H':
low = (guess + 1);
high = (high - guess);
break;
case 'L':
high = guess;
break;
default:
break;
}
I guess what I'm not understanding is how rand() truly works. Like I understand the whole
rand() % 100 //range of 0 and 99
rand() % 100 + 1 // range of 1 and 100
But I want to know how I can change the parameters so that I get
rand() % max + min // range of min and max
while I constantly change and update the values for max and min.
Aucun commentaire:
Enregistrer un commentaire