Get rand() to guess higher and lower

This program accepts a users secret number between 0-100 and the computer will guess the number. If it's high or low the user indicates that to the computer, and the computer will guess again using that last response as a filter.

I'm having trouble generating the correct number at line 24 where the user says the number was too low. I have the rest correct I think, just need help at line 24.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 5_BracketingSearch.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <stdio.h>      /* printf, scanf, puts, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>	  /* time */
using namespace std;

int genGuess(int highorlow)
{
static unsigned guess = 0;
static unsigned guessHigh = 0;
	if (highorlow == 0) {
		srand(unsigned(time(NULL)));
		guess = rand() % 100 + 1;
		cout << guess;
	} else if (highorlow == 1) {
		guess = rand() % guess;
		guessHigh = guess;
	} else if (highorlow == 2) {
		guess = rand() % guessHigh + guess;		// PROBLEM IS HERE. Need to get randNum between last randNum and 100 range.
	}
return guess;
}

bool hotorcold(int num, int guess, int& highorlow)
{
	int userResp = 0;
	if (num != guess) {
		cout << "The computer guessed " << guess << ".\n\n";
		cout << "Was this too high or too low?\n";
		cout << "1.) Too High\n";
		cout << "2.) Too Low\n";
		cin >> userResp;
		highorlow = userResp;
		return false;
	}
	return true;
}

int main()
{
	int secret = 0;
	int compResp = 0;
	bool position = false;
	int highorlow = 0;
	cout << "Bracketing Search\n";
	cout << "Pick a number between 1 and 100 for the computer to guess \n\n";
	cout << "Input secret number: ";
	cin >> secret;
	while (position == false){
		compResp = genGuess(highorlow);
		position = hotorcold(secret, compResp, highorlow);
	}
		cout << "The computer has guessed your number!" << endl;
}
Last edited on
To get a random number in [x,y] try the following:
(rand()%(y-x))+x
Great it works... more or less... now if I could just get this thing to find the secret number within 7 tries...

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// 5_BracketingSearch.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <stdio.h>      /* printf, scanf, puts, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>	  /* time */
using namespace std;

int genGuess(int highorlow)
{
static unsigned guess = 0;
static unsigned guessHigh = 100;
static unsigned guessLow = 100;
	if (highorlow == 0) {
		srand(unsigned(time(NULL)));
		guess = rand() % 100 + 1;
		cout << guess;
	} else if (highorlow == 1) {
		guessHigh = guess;
		guess = rand() % guess;
	} else if (highorlow == 2) {
		guessLow = guess;
		guess =  (rand()%(guessHigh-guessLow))+guessLow;
	}
return guess;
}

bool hotorcold(int num, int guess, int& highorlow)
{
	int userResp = 0;
	if (num != guess) {
		cout << "The computer guessed " << guess << ".\n\n";
		cout << "Was this too high or too low?\n";
		cout << "1.) Too High\n";
		cout << "2.) Too Low\n";
		cin >> userResp;
		highorlow = userResp;
		return false;
	}
	return true;
}

int main()
{
	int secret = 0;
	int compResp = 0;
	bool position = false;
	int highorlow = 0;
	cout << "Bracketing Search\n";
	cout << "Pick a number between 1 and 100 for the computer to guess \n\n";
	cout << "Input secret number: ";
	cin >> secret;
	while (position == false){
		compResp = genGuess(highorlow);
		position = hotorcold(secret, compResp, highorlow);
	}
		cout << "The computer has guessed your number!" << endl;
}
Last edited on
You need to look at your algorithim in genGuess more closely.
You want to start with guessLow at 1.
Rather than being a random number, your guess should be exactly 1/2 way between guessLow and guessHigh. So in all three cases in genGuess,
1
2
 
  guess = guessLow + ((guessHigh - guessLow) /2);

That will narrow the range as quickly as possible.
Magic! I read somewhere that this process takes cutting in /2's. Didn't know how to implement it though.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// 5_BracketingSearch.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <stdio.h>      /* printf, scanf, puts, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>	  /* time */
using namespace std;

int genGuess(int highorlow)
{
static unsigned guess = 0;
static unsigned guessHigh = 100;
static unsigned guessLow = 1;
	if (highorlow == 0) {
		srand(unsigned(time(NULL)));
		guess = guessLow + ((guessHigh - guessLow) /2);
		cout << guess;
	} else if (highorlow == 1) {
		guessHigh = guess;
		guess = guessLow + ((guessHigh - guessLow) /2);
	} else if (highorlow == 2) {
		guessLow = guess;
		guess = guessLow + ((guessHigh - guessLow) /2);
	}
return guess;
}

bool hotorcold(int num, int guess, int& highorlow)
{
	int userResp = 0;
	if (num != guess) {
		cout << "\n\nThe computer guessed: " << guess << ".\n";
		cout << "Remember, your secret number was: " << num << ".\n\n";
		cout << "Was this too high or too low?\n";
		cout << "1.) Too High\n";
		cout << "2.) Too Low\n";
		cin >> userResp;
		highorlow = userResp;
		return false;
	}
	return true;
}

int main()
{
	int secret = 0;
	int compResp = 0;
	bool position = false;
	int highorlow = 0;
	cout << "Bracketing Search\n";
	cout << "Pick a number between 1 and 100 for the computer to guess \n\n";
	cout << "Input secret number: ";
	cin >> secret;
	while (position == false){
		compResp = genGuess(highorlow);
		position = hotorcold(secret, compResp, highorlow);
	}
	cout << "The computer has guessed your number!" << endl;
}


Thanks AbstractAnon and ZHuge.
Last edited on
Divide and conquer! lol

Get the range and guess in the middle.
Topic archived. No new replies allowed.