guess the number game using random number generator

May 7, 2013 at 6:22pm
Make a "guess the number game", in which the user enters a number and the computer has to guess that number.

My code does works but can you give me a better way to do this.

#include<iostream>
#include<conio.h>
#include<cstdlib>
#include<ctime>

using namespace std;

int main()
{

int count=0;
int playerNumber;
int randomNumber;
int computersGuess;


cout<<"Lets play \n";
cout<<endl;
cout<<"Hey player, Choose your number \n";
cin>>playerNumber;

while(1)
{

srand(static_cast<unsigned int>(time(0)));//seed
randomNumber = rand();
computersGuess = (randomNumber % 100) + 1;
count++;
cout<<computersGuess<<"\n";

if(computersGuess == playerNumber)
{
cout<<"You chose : "<<computersGuess<<"\n";
cout<<"It took me "<<count<<" tries \n";
break;
}

}

getch();
}


May 7, 2013 at 6:25pm
Don't seed the random number generator inside the loop. Do it once at the beginning of the program.

Put the computer guess generator in a separate function.

Generalize your random number generation parts with parameters to indicate the range in which you want your random number to fall.
May 7, 2013 at 6:28pm
ya , my range is from 1 - 100.
but if i don't seed it everytime how will i get different random numbers.

and could u tell me a way, to guess the number faster.
May 7, 2013 at 6:30pm
well, thanks a lot, i just seeded the thing outside loop and it guesses the number in less tries.
but why so?
May 7, 2013 at 6:44pm
When you seed the random number generator, you're telling it where to start in a pseudo-random sequence, and the 'time' you seed it with has a resolution of only one second. Basically, your program guesses the same number over and over again for a full second until the time(0) property changes and it seeds using a new number. Try this code and you'll see that the computer guesses the same thing over and over, and only changes its guess once a second or so.
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
#include <iostream>
#include <cstdlib>
#include <ctime>

void badFunc()
{
    int playerNumber = 12;
    int count = 0;
    while(1)
    {
        srand(static_cast<unsigned int>(time(0)));//seed
        int randomNumber = rand();
        int computersGuess = (randomNumber % 100) + 1;
        count++;
        std::cout<<computersGuess<<"\n";

        if(computersGuess == playerNumber)
        {
            std::cout<<"You chose : "<<computersGuess<<"\n";
            std::cout<<"It took me "<<count<<" tries \n";
            break;
        }

    }
}

int main()
{
    badFunc();
    return 0;
}
May 7, 2013 at 6:50pm
Here's what I meant by parameterizing your random number generation stuff. Now you can generate numbers within a flexible range of positive 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
#include <iostream>
#include <cstdlib>
#include <ctime>

int getRandomNumber(unsigned int lowEnd, unsigned int highEnd)
{
    //if the range is one number, don't bother with rand
    if(lowEnd == highEnd) return lowEnd;

    //swap the values if the user passed them in the wrong order
    if(highEnd < lowEnd)
    {
        unsigned int temp = highEnd;
        highEnd = lowEnd;
        lowEnd = temp;
    }

    //stick our desired range into a variable for readibility
    //add one at the end to avoid a fencepost error
    unsigned int range = (highEnd - lowEnd) + 1;

    //generate a number within our range and shift it up
    //based on the lowest number we want to generate
    return (std::rand() % range) + lowEnd;
}

int main()
{
    //seed only once!
    std::srand(std::time(nullptr));

    for(int i = 0; i < 10; ++i)
    {
        //generate a random number between 7 and 13
        std::cout << getRandomNumber(7, 13) << "\n";
    }
    return 0;
}
Last edited on May 7, 2013 at 6:52pm
May 11, 2013 at 11:32am
thanks.
Topic archived. No new replies allowed.