You are using a version without Ads of this website. Please, consider donating:

### Computer Improved Guessing Inteligence

Why isn't this working? I know its not right but I thought it out and still not working. I'm trying to make it so if the computer has guessed a lower number than the secret one it will make it so the minimum number is at least greater than the last one. Thank you!

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` ``````#include #include #include using namespace std; int getNumber(); int main() { // Computer try to guess my secrect number! int numberOfAttempts = 0; // The number of trys it took the computer to guess the secret number int computerGuess = 0; // The number the computer has gussed int min = 1; // Min range to guess int max = 100; // Max range to guess // Get the players number to be guessed by the computer cout << "Secret number: "; int numberToBeGussed = getNumber(); // The number to be guessed by the computer srand((unsigned int) time(0)); // Seed the random number generator do { if (numberOfAttempts == 0) // First guess random 1-100 computerGuess = (rand() % max) + min; if (numberOfAttempts != 0 && computerGuess < numberToBeGussed) // If guess was less then secret number set minimum higher then last { min = computerGuess + 1; computerGuess = (rand() % max) + min; } else if (numberOfAttempts != 0 && computerGuess > numberToBeGussed) // If guess was higher then secret number set max number less then last { max = computerGuess - 1; computerGuess = (rand() % max) + min; } cout << "Computer guess: " << computerGuess << endl; numberOfAttempts++; // Increment the number of trys the computer has made } while (computerGuess != numberToBeGussed); // While the computer has no guessed the secret number keep looping return 0; } int getNumber() { int number = 0; cin >> number; return number; }``````

Program output:
 ``1234567891011121314151617181920212223242526272829303132`` ``````Secret number: 50 Min: 1, Max: 100, Computer guess: 8 [L]Min: 9, Max: 100, Computer guess: 29 [L]Min: 30, Max: 100, Computer guess: 54 [G]Min: 30, Max: 53, Computer guess: 32 [L]Min: 33, Max: 53, Computer guess: 55 [G]Min: 33, Max: 54, Computer guess: 75 [G]Min: 33, Max: 74, Computer guess: 89 [G]Min: 33, Max: 88, Computer guess: 115 [G]Min: 33, Max: 114, Computer guess: 103 [G]Min: 33, Max: 102, Computer guess: 134 [G]Min: 33, Max: 133, Computer guess: 110 [G]Min: 33, Max: 109, Computer guess: 102 [G]Min: 33, Max: 101, Computer guess: 126 [G]Min: 33, Max: 125, Computer guess: 131 [G]Min: 33, Max: 130, Computer guess: 66 [G]Min: 33, Max: 65, Computer guess: 94 [G]Min: 33, Max: 93, Computer guess: 107 [G]Min: 33, Max: 106, Computer guess: 127 [G]Min: 33, Max: 126, Computer guess: 138 [G]Min: 33, Max: 137, Computer guess: 92 [G]Min: 33, Max: 91, Computer guess: 122 [G]Min: 33, Max: 121, Computer guess: 34 [L]Min: 35, Max: 121, Computer guess: 112 [G]Min: 35, Max: 111, Computer guess: 44 [L]Min: 45, Max: 111, Computer guess: 147 [G]Min: 45, Max: 146, Computer guess: 163 [G]Min: 45, Max: 162, Computer guess: 125 [G]Min: 45, Max: 124, Computer guess: 50 Process returned 0 (0x0) execution time : 1.203 s Press any key to continue.``````
Last edited on
Did you mean to add min to the random number in both cases ?
It isn't working because your random number generation doesn't correctly choose between 2 numbers like you want it to.

It should be set up like
` (rand()%(max-min))+min; `

In this case if both max and min are the same number it will cause an error because rand will be trying to work with 0. So maybe change your code to something like this.

 ``123456789`` ``````if(max - min == 0) { computerGuess = min; } else { computerGuess = (rand() % (max - min)) + min; }``````

(Although there might be other ways to solve that)
Thanks man it works now. Why is it setup like (rand()%(max-min))+min;
I thought it was (rand() % MAX) + MIN;
(rand() % max) = 0 to max (not including max)
(rand() % max) + min = 0 + min to max + min (not including max + min)
(rand() % (max - min)) + min = 0 + min to max - min + min(not including max - min +min)

max = 10; min = 2:

1. 0 to 9(including 9 (=max - 1))
2. 2 to 11 (12-1 = 11)
3. 2 to 9 (10-1 = 9)
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: