I am writing a game where the computer chooses a random number that a user has to guess. Then, It is the user to choose a number (he just guesses a number) that the computer has to guess. The problem is that the rand() function that i use in the second part of the game sometimes provides a number outside the new range. for instance if the new range is low=4 and high=10, rand() provides 12, which is not correct. I am trying to understand why that error, but i cannot find it. I am wrinting in Dev-C++. The code is right below followed by the output. Thanks for your help and time.
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main()
{
int computer_guess, nbr, nbrguesses, count_user_guess=0, count_computer_guess=0;
char user_comparison;
int low=1, high=99; //initialize low to the lower boundary and high to the higher boundary
srand(time(0));
nbr=rand()% 99 + 1; //computer chooses a random number between 1 and 99...
do
{
cout << "guess a number now! " << endl;
cin >> nbrguesses; //user enters the number he guessed...
if ( nbrguesses>nbr)
cout << "too big" <<endl;
else if ( nbrguesses<nbr)
cout << "too small" << endl;
else if ( nbrguesses==nbr)
cout << " you found the correct number! " <<endl;
count_user_guess++; //count the number of guesses from the user
}
while (nbrguesses!=nbr);
cout << "you tried " << count_user_guess <<" times to find the right number " << endl<<endl;
cout << "----------------------------------------------------------------------" << endl;
cout << "Now, the computer will guess your number " << endl;
do
{
srand(time(0));
computer_guess=rand()% high + low; //computer guesses the number
cout << "computer guess: " << computer_guess <<endl;
cout << "is it the correct number?" << endl;
cin >> user_comparison; //if user enter
if (user_comparison=='>') // character '>' it means the number guessed by computer is too big
{
cout << "too big" <<endl;
high= computer_guess-1; //high is given a new boundary
cout << "***Current value of high = " << high << " and low = " << low << endl; //display current boundaries
}
else if ( user_comparison=='<') //the number guessed by computer is too small
{
cout << "too small" << endl;
low= computer_guess+1; //low is given a new boundary
cout << "***Current value of low = " << low << " and high = " << high << endl; //display current boundaries
}
else if ( user_comparison=='=')
cout << "Computer found the correct number! " <<endl;
count_computer_guess++; //count number of guesses from computer
}
while (user_comparison!='=');
cout << "The computer tried " << count_computer_guess <<" times to find the right number " << endl;
cout << "The Game is over now, have a good day!" << endl;
getch();
return 0;
}
//**************Output******************************************
guess a number now!
50
too big
guess a number now!
25
you found the correct number!
you tried 2 times to find the right number
----------------------------------------------------------------------
Now, the computer will guess your number
computer guess: 11
is it the correct number?
>
too big
***Current value of high = 10 and low = 1
computer guess: 3
is it the correct number?
<
too small
***Current value of low = 4 and high = 10
computer guess: 12
is it the correct number?
You have to replace
by
In your example you have low = 4 and high = 10. But then you want a random number between 0 and 6 and add 4. Not one between 0 and 10, since then you could get results up to 14. If you dont want to include the high bound (Interval [low,high[ instead of [low,high] ) you have to omit the +1 in the brackets.
Why
rand() returns a pretty big integer number. And we want get a random number in the interval [a,b] from that.
If we take rand()%5, we get a numer which is 0,1,2,3 or 4, so from the interval [0,4]. In general rand()%C gives a random number in the interval [0,C-1].
If we add a constant number, e.g. Rand()%C + D, the interval is shifted: [D,C-1 +D].
So back to the problem, we want the interval to be [a,b]. Therefor we want a=D for the lower bond and C-1+D = b for the upper. This we can transform to C = b-D+1 = b-a+1.
So we want to use rand()%(b-a+1) + a.
Hope this explains a bit how things work.