Hi , i'm trying to do an exercise where the computer has to guess the random number from the range of 1-1000. Currently this is the way i have it set up as. I divide that range in half and store it in guess and then check it against the random number. I'll continue to mod by 2 and compare it against random until i have a match. I dont know if this is the better way to go, any suggestions ? Thanks.

You might also want to do something if the guess is too high, too low or correct.

example:

you enter a random number say => 765

The computer guesses 500 which is too low so the next number is one half of the high range => 750...and this continues, taking the top half or bottom half of the remaining range until it hits the number.

>> mod by 2

What's this mean? Be precise. I assume you mean you split the possible range in half, pick one side, then guess the midpoint of that side, then do it again and again till you find it. That's a binary search. That's fine, but you left something out of your problem statement. Presumably you get a hint as to whether you're right, too high, or too low after you guess, but you didn't explicitly state that. If that's the case, I can't think of a better method than what you describe. If you don't get that hint or if you get some other hint, all bets are off. But I'm assuming that's the scenario, in which case you have the right approach.

srand(time(NULL))
rand()
#include <cstdlib> \\ Optional some times...

Need more help? I can't do much more....

Thanks for the replies, i haven't written the code yet, just wanted to get some input i'll try to implement the solution sometime today.

Thanks for the replies, i haven't written the code yet, just wanted to get some input i'll try to implement the solution sometime today.

I found that random number generating can be quite tricky some times. It might help for you to try other kinds of programs first, but working with random doesn't have to be limited to C++'s standards there are external libraries able to produce random number generations.

Hello , well i thought i had this figured out , but solution i came up with don't work. Here's what i was trying to accomplish. Lets say that the random number that was generated is 789 out of 1000. Im supposed to let the program pick this number out of ten attempts. Exercise states that program should be able to pick this number in ten attempts. I was doing it in on paper for a while and it seemed to work , but it doesn't work for all scenarios. So what i was doing is i split the first guess in half by the maximum range which gives me 500 and then i compare it to the random number if its less then i do the following steps.

pseudo code
guess /= 2;
attempt 1: 500 / 2 = 250 
guess += 250  // 750
attempt 2: 250 / 2 = 125
now 125 + 750 is greater then random so i skip it and so on.
attempt 3: 125 / 2 = 62  782 Good
attempt 4: 62 / 2/ = 31  skipped
attempt 5: 31 / 2 = 15  skipped
attempt 6: 15 / 2 = 7   789 Done,

I dont think im on the right track. Any suggestions ?

You seem to think that intermediates are attempts that is not the case,
I don't understand how you can skip that test of 875, it is a guess, and too high a guess.

e.g.

computer value: 789

minValue=1;
maxValue=1000;
for(int i=0;i<10;i++)
  { 
    guess=(maxValue+minValue)/2;
    // set maxValue minValue based on the result
    if (high)
      minValue=guess;
    else
      maxValue=guess
  }

That would give:

attempt 1:  500   low
attempt 2:  750   low 
attempt 3:  875   high
attempt 3:  812   high
attempt 4:  781   low
attempt 5:  796   high
attempt 6:  788   high
attempt 7:  792   high
attempt 8:  790   low 
attempt 9:  789   Success.

With only one value to find and only that information, and the cost of guessing high/low being the same, then that the optimal way to find the solution.

Well, right i was doing the same approach by me saying that i was ignoring a guess i meant that i wasn't interested in incorrect guess therefore i just didnt update the guess. I still counted however how many times it took him to get tehre. We have the same algorithm which i dont think works for every value. For example 871, it fails

I must be missing a step in my code, i think algorithm should work, i'll have to see where my error is and i'll post what i come up with.

I had the wrong approach initially. Now the code works had to change my logic around a bit.

#include "stdafx.h"
#include <iostream>
#include <ctime>

using std::cout;
using std::endl;
using std::cin;

int high_low(int,int,int);

int main()
{
	srand(int(time(NULL)));
	int random = 1 + rand() % 1000;
	int guess = 1000, counter = 0;
	int low = 0, high = 0;

	cout << "Random #" << random << endl;

	while( guess != random && counter++ < 10)
	{
		cout << "Guess # " << counter << " number: " << guess << endl;
		
		if( guess > random )
		{
			cout << "Lower...\n";
			high = guess;
			guess = high_low(guess,high,low) + low;
		}
		else
		{
			cout << "Higher...\n";
			low = guess;
			guess += high_low(guess,high,low);
		}
	}
	cout << "Number you guessed is: " << guess << " in "
		 << counter << " tries";
	
	cin.get();
	cin.get();
	return 0;
}

int high_low(int guess,int high,int low)
{
	return (high - low) / 2;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.