Guess My Number

How do I make this so it wont guess the same number more than once? In reality, it should only have to guess a maximum times of 100 because the numbers range from 1-100. When I run the program it can take it 500 times to get the right answer.

I want to eliminate the possibility of it guessing the same number twice
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
41
42
43
  #include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
	srand(static_cast<unsigned int>(time(0)));
	int computerGuess;
	int myNumber;
	int tries = 0;

	cout << "\tWelcome to Guess My Number\n\n";
	cout << "Enter a number 0-100 and the computer will try to guess it." << endl;

	cout << "Enter your number: ";
	cin >> myNumber;
	
	do
	{
		computerGuess = rand() % 100 + 1;
		cout << "\nThe computer's guess is: " << computerGuess << endl;
		++tries;

		if (computerGuess > myNumber)
		{
			cout << "Guess was too high" << endl;
		}

		else if (computerGuess < myNumber)
		{
			cout << "Guess was too low" << endl;
		}

		else
		{
			cout << "You guess it! It took you " << tries << " tries" << endl;
		}
	} while (computerGuess != myNumber);

	return 0;
}
closed account (2AoiNwbp)
In reality, it should only have to guess a maximum times of 100 because the numbers range from 1-100

Not necessarilly, it`s a random process, you could get 10 times each number till you get the result, and it would sum up to 1000 trials!!
Nevertheless, if you want to do it anyway, I think you could save each guess in a vector of 100 elements and check this vector each time to see if the number is already there. But it would be like cheating, because you need a trial to check, no matter you count it or not...

regards,
Alejandro
Last edited on
You are going to need some way to keep track of the guessed numbers. What I would do though is just create an array of the 100 numbers then randomly sort it and iterate through it till it finds the correct value.


1
2
3
4
5
6
std::vector<int> guesses(100);
std::generate(guesses.begin(), guesses.end(), [&](){static int count = 1; return count++;});

std::random_shuffle(guesses.begin(), guesses.end());

//now just iterate over guesses till you found the correct answer. 

http://ideone.com/DxHX6U

aabuezo wrote:
, I think you could save each guess in a vector of 100 elements and check this vector each time to see if the number is already there
If you are doing that way you should probably use a std::set instead of a std::vector
Last edited on
closed account (2AoiNwbp)
Not necessarilly, it`s a random process, you could get 10 times each number till you get the result, and it would sum up to 1000 trials!!

And this is IMHO, were resides the usefulness and beauty of randomness.

If, on the other hand, what you want is to find the number in the minimun number of trials, what I would use is binary search.

regards,
Alejandro
beauty of randomness.
well...technically it's not random it's just following a pattern. I think what he wants is to pick a unique pseudo random number each time from a list of numbers. Not an arbitrary pseudo random each time.
closed account (2AoiNwbp)
well...technically it's not random it's just following a pattern

I like that! Where can I find how it works??
I normally use rand() with big numbers, so I didn't realize of that pattern.

regards,
Alejandro
Last edited on
Just search pseudo random number generators(PRNG) or linear congruential generator(LCG). http://en.wikipedia.org/wiki/Linear_congruential_generator
closed account (2AoiNwbp)
Thanks giblit, I`ll take a look
Topic archived. No new replies allowed.