Gen ranNum and making the computer guess my number.

Pages: 12
Hello, I need help creating a program where the user is asked to think a number between 1 and 10, and the computer tries to (randomly) “guess” the number. The user should provide clues to the computer so that the random number the computer generates keeps getting closer to the answer in each step. The following is a sample run of the program:

Think of a number between 1 and 10. Is 3 the number you are thinking of? Enter h if the number you are thinking of is higher, l if it is lower or c if it is correct.

h

Is 5 the number you are thinking of? Enter h if the number you are thinking of is higher, l if it is lower or c if it is correct.

h

Is 9 the number you are thinking of? Enter h if the number you are thinking of is higher, l if it is lower or c if it is correct.

c

I guessed your number in 3 steps.


I really have no idea where to start. Any help will be greatly appreciated. Thanks
Well, to start with, you'll need to generate a random number. To do this, you'll first need to seed the random number generator with something variable - this is usually done with the current time. Then, to constrain the results, we can do a modulus of our desired maximum, plus the desired minimum. So, to put it all together:

1
2
3
4
5
6
7
8
#include <cstdlib> //needed for rand() and srand()
#include <ctime> //needed for time()

int main() {
    int max = 10, min = 1;
    srand(time(0)); //seed the random number generator
    int r = (rand() % max) + min;
}


From there, it should just be a case of changing you're min/max constraints based on the user's input.
Last edited on
Ok. Makes a little bit more sense. Thanks
is it solved?
Not yet. This all I have so far:


#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

int main()
{
int numberOfSteps = 0;
const int magic = 'c';
int steps = 0;
char number = ' ';

//enter number
cout << "Think of a number between 1 and 10.";
cin >> number;

while (number != '7')

numberOfSteps =+ 1;

cin >> number;

{
int max = 10, min = 1;
srand(time(0)); //seed the random number generator
int r = (rand() % max) + min;
}
@mora14,
please put your code in code tags.
should i share the code to you or tell the algorithm?
I don't know how. I'm new to this page, but any help is good. Thanks
so, here is the required program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <conio.h>


int main()
{
    randomize();
    int num,random_num,numd,lowerlimit=0,upperlimit,no_of_times=0; char choice='a';
    cout<<"\n enter num 0-10  ";
    cin>>num;
    upperlimit=num+1;
    while(choice!='c')
    {

        random_num=random(upperlimit-lowerlimit+1)+lowerlimit;
        cout<<"\n is your no h/l/c  than "<< random_num <<" ? ";
        cin>>choice;
        switch(choice)
        {
            case 'h' :  lowerlimit=random_num;
            break;
            case 'l' :  upperlimit=random_num;
            break;
            default : ;
        }
        no_of_times++;
    }

    cout<<"\n your no. is "<<num<<" and i found it in "<<no_of_times<<" steps \n";
    system("pause");
    return 0;
}


try running in compiler other than C::B

Here are 2 ways of doing that:

1.One without random numbers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int main()
{
    int lower,higher,maxnr,answer,myguess;
    cout<<"Think of a number between 1 and ";cin>>maxnr;
    higher=maxnr+1;lower=1;
    while(true){
    myguess=(higher+lower)/2;
    cout<<"I think it is "<<myguess<<'\n';
    cout<<"Is this the number ?\n";
    cout<<"1.Lower\n";
    cout<<"2.This is it !\n";
    cout<<"3.Higher\n";
    cin>>answer;
    if(answer==1) higher=myguess;
    if(answer==2) {cout<<"It was fun wasn't it\n"; return 0;}
    if(answer==3) lower=myguess;}
    return 0;
}




And one with random numbers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void pause()
{
    //Prevents the application from exiting imediatly ...
    cout<<endl<<"Press any key to continue . . .";
    cin.ignore();
    cin.get();
}
int main()
{
    int maxnumber,minnumber=1,answer,number,aux;
    srand(time(0));
    cout<<"Think of a number between 1 and ";cin>>maxnumber;
    ++maxnumber;
    while(answer!=2){
    aux=maxnumber-minnumber;
    if(aux){
    number=rand()%aux+minnumber;}
    else {cout<<"You cheated !\n"; pause(); return 0;}
    cout<<"I think it is "<<number<<endl;
    cout<<"It is this the number ?\n\n";
    cout<<"1.Less\n";
    cout<<"2.This is it!\n";
    cout<<"3.Higher\n\n";
    cin>>answer;cout<<endl;
    if(answer==1) {
    maxnumber=number;
    }
    if(answer==3){
    minnumber=number+1;
    }
    if(maxnumber-1==minnumber) {cout<<"I found your number.It is "<<minnumber<<endl; pause(); return 0;}
    }
    pause();
    return 0;
}
@aalok
Don't use conio.h, its non-standard and can cause problems with most compilers (almost everything used nowadays except VC++). Also, this is a C++ program, so use the C++ headers, not the C ones (i.e. cstdlib and ctime).

@OP
Don't seed the random number generator in a loop. Just seed it once, normally at the starto of your main function. If you are using characters, prefer to use char, unless you are using getchar() (which you shouldn't be). Also, your while loop is only infinitely looping over whether the number is '7', and then increasing the number of steps, creating an infinite loop. Consider adding {braces} around the whole area meant to be covered by the loop.
Any 0ne plz tell me, Is it must to use binary search in this program ??
NT3 conio.h is pretty standard on windows, but the thing is that he didnt needed to use it anyawy! He didint used any conio.h function on his program.He could have used _getch() instead of the crap system ("pause"), but he didnt ! Also he used borland specific macros such as randomize().Oh and he forgot to put using namespace std; after #include <iostream>
None of these programs are working for me. Any more tips? I'm a beginner, so I don't understand much.
What my programs dont work ????
No. It starts the program, but it doesn't try guessing the number. The program just shuts down. I use Dev- C++, I don't know if that has something to do with it.
@SorinAlex,
I used Borland's for making this code coz my Code::Blocks got hanged, so I used randomize() and all that.
And I don't think Borland's is for C++ hence, no need of using namespace std;

@mora14,
Try my code in a C compiler if you have one, and I'll upload a C++ code soon.
@mora14,
try this one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <conio.h>
using namespace std;

int main()
{

    int num,random_num,numd,lowerlimit=0,upperlimit,no_of_times=0; char choice='a';
    cout<<"\n enter num 0-10  ";
    cin>>num;
    upperlimit=num+1;
    while(choice!='c')
    {

        random_num=rand()%(upperlimit-lowerlimit+1)+lowerlimit;
        cout<<"\n is your no h/l/c  than "<< random_num <<" ? ";
        cin>>choice;
        switch(choice)
        {
            case 'h' :  lowerlimit=random_num;
            break;
            case 'l' :  upperlimit=random_num;
            break;
            default : ;
        }
        no_of_times++;
    }

    cout<<"\n your no. is "<<num<<" and i found it in "<<no_of_times<<" steps \n";
    return 0;
}
@mora14 I don't know i use Code::Blocks.But when you start the the program it should say something like "Think of a number between 1 and " and then you have to write number ,for example 954.Then you think of a number the program will try to guess it ranging from 1 to 954(the number you entered)

Then it should say something like :

I think it is 54 (for example)
It is this the number ?

1.Less
2.This is it!
3.Higher

and then you write 1,2 or 3.The program then will narrow the range which it guesses the number and guess another number.


----------------------------------------------

For example:

Think of a number between 1 and (enter a number here)

// I enter 20
//and I think at 13
//The program will now generate a random number between 1 and 20.


I think the number is 17.
Is this the number ?
1.Less
2.This is it!
3.Higher

//I enter 1 since the number i though of is 13

//The program will now generate a random number between 1 and 16

I think the number is 3.
Is this the number ?
1.Less
2.This is it!
3.Higher


//I enter 3 since i though of is 13
//The program now generates a number between 4 and 16

I think the number is 5.
Is this the number ?
1.Less
2.This is it!
3.Higher

//I enter 3
//The program generates number between 6 and 16

I think the number is 12.
Is this the number ?
1.Less
2.This is it!
3.Higher

//I enter 3
//The program generates number between 13 and 16

I think the number is 13.
Is this the number ?
1.Less
2.This is it!
3.Higher

//I enter 2 since this is number i thought of.

This is it hope you'll understand it !
Last edited on
@SorinAlex

It works, but not exactly right. If I type 10 first( so it can generate a number between 1 & 10) it does it correctly, but when it finally guesses my number correctly, it shows it wrong. For example, I choose 5. It says, "your no. is 10 and i found it in 5 steps", instead of saying, "your no. is 5 and I found it in x steps". This gives me a good idea to work off of though. Thanks!
Pages: 12