Jump to content
  • Advertisement
Sign in to follow this  
JeremyYox

Random Numbers

This topic is 2600 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Question,

I'm rewriting a Guess My Number program to have the comp guess a value input by the user, and I finally got it working (a simple break; kept me busy for over an hour). My question is a two parter: how could I go about making it so that the program understands to 'guess' in a given range (higher/lower than x)? If its overly complex I'll pass for now, I'm just starting and was curious because having the program say:

My guess is 45, is it right? Nope, too low! My next guess is 97! ....etc seems strange. :)

Program below in the even answers are context sensitive:
// Guess My Number Redux
// Reprogram the Guess My Number program so the user chooses a number for the computer to guess.

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
cout << "\tWelcome to Guess My Number Redux!\n\n";

int myNumber = 0;

cout << "Input a number between 1 and 100: ";
cin >> myNumber;
cout << "You chose: " << myNumber << " lets see how many tries it takes for me to guess it!\n\n";

int tries = 0;
int guess = 0;

do
{
int guess = rand() % 100 + 1; //computer's guess, random number between 1 and 100 assigned to guess.

cout << "My guess is: " << guess << " but is it right?\n";

if (guess > myNumber)
{
cout << "Nope, too high!\n\n";
++tries;
}
else if (guess < myNumber)
{
cout << "Nope, too low!\n\n";
++tries;
}
else if (guess == myNumber)
{
cout << "Got it! Your number was " << guess << "! It only took me " << tries << " to guess it!\n";
break;
}
} while (guess != myNumber);
return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
you simply need two variables. HighBounds and LowBounds. substitute 100 for HighBounds and 1 for lowBounds. once the program has guessed if it is too low then set the lowBounds to the guess+1 if too high then set highBounds to the guess-1. then re guess the number using the variables.




So if you pick 40 and it guesses a random number between 1 and 100 and guesses 10 then the next guess will be between 11 and 100

the computer then guesses 60 so guesses again between 11 and 59.

Share this post


Link to post
Share on other sites
You can try something like this:

//int guess = rand() % 100 + 1;
int guess = rand() % (highest - lowest) + lowest;


Then you start with highest at 101 and lowest at 1. If the guess is too low, update lowest when you do ++tries, and the next number chosen will never be lower than that. Similar when the guess is too high for the highest number.

Share this post


Link to post
Share on other sites
store a max guess and a min guess (would be 0 to some max number). Generate a number in that range. If the guess was too low, then set the min to that value; if the number was too high - set the max to that value. Repeat, generate a number in the range etc. Eventually min == max, (or you got the guess right). Be sure to check the case when someone says the number is too low/high even though it can't possibly be (because their cheating).

Share this post


Link to post
Share on other sites
Another note, you may want to initialise the random generator. I believe you use srand(time(0)); in c++ (i am a c# developer) otherwise I think you may find that rand generates the same number each time( or at leats at the same end of the range in this case (i may be wrong though)

Share this post


Link to post
Share on other sites

Another note, you may want to initialise the random generator. I believe you use srand(time(0)); in c++ (i am a c# developer) otherwise I think you may find that rand generates the same number each time( or at leats at the same end of the range in this case (i may be wrong though)



Ahh you're right, I forgot the seed. initially it was meant to seed off the system clock. Thanks for the catch.

I'm trying the other methods now to see how they all work, I'm learning still so just reading the code is still like reading code. :)

Share this post


Link to post
Share on other sites

You can try something like this:

//int guess = rand() % 100 + 1;
int guess = rand() % (highest - lowest) + lowest;


Then you start with highest at 101 and lowest at 1. If the guess is too low, update lowest when you do ++tries, and the next number chosen will never be lower than that. Similar when the guess is too high for the highest number.



This worked beautifully and was really pretty simple once I worked my way through it. I'm going to post the update just to ensure that I implemented it the way you were all trying to explain. Thanks again!

// Guess My Number Redux
// Reprogram the Guess My Number program so the user chooses a number for the computer to guess.

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
srand(static_cast<unsigned int>(time(0))); //seed random number generator

cout << "\tWelcome to Guess My Number Redux!\n\n";

int myNumber = 0;

cout << "Input a number between 1 and 100: ";
cin >> myNumber;
cout << "You chose: " << myNumber << " lets see how many tries it takes for me to guess it!\n\n";

int tries = 0;
int guess = 0;
int min = 1;
int max = 101;

do
{
int guess = rand() % (max - min) + min; //computer's guess, random number between 1 and 100 assigned to guess.

cout << "My guess is: " << guess << " but is it right?\n";

if (guess > myNumber)
{
cout << "Nope, too high!\n\n";
++tries;
max = guess;
}
else if (guess < myNumber)
{
cout << "Nope, too low!\n\n";
++tries;
min = guess;
}
else if (guess == myNumber)
{
cout << "Got it! Your number was " << guess << "! It only took me " << tries << " to guess it!\n";
break;
}
} while (guess != myNumber);
return 0;
}


Share this post


Link to post
Share on other sites
Just a small thing, 'min' is used as the lowest guessable number, so you could set it to 'guess + 1' to make sure the same number won't be guessed again.
Perhaps it makes more sense to use int guess = rand() % (max - min + 1) + min for consistency, as that would make max be the highest actually guessable number just as min is the smallest.

Share this post


Link to post
Share on other sites
Hidden
Also you'll never be ever to guess the number generated... every loop your randomizing the number again

Share this post


Link to post
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!