Problem understanding rand()

So i'm doing some of the beginner exercises that was posted on this website

 
http://www.cplusplus.com/forum/articles/12974/ 


namely the "bracket search" i'm on the 4 star challenge and i'm having some problems :<

challenge:

 
Pick a number and have the program guess it. modify the program so that no matter what number the user thinks of (1-100) the computer can guess it in 7 or less guesses.



What i'm having problem with is getting rand() to do what i want. I want it to search in a specific range.

I understand it very basic like for example if i were to write

rand()% 100 + 1;

this would generate a number within the range 1-100 the problem occurs when i try to have ranges like from 40-50

i know the code for it would be rand()% 10 + 40;

But i don't understand how i can get the program to update the specific range by itself.

so for example it would be like this

user has a number between 1-100 let's say he picks 19

the program generates the number 29

this number is higher than the what the user inputed

so the program should now search 1-29

the program generates 15

this number is lower than the one the user picked

so the program now searches 15-29

and so on til the program finds the correct number. but i have no idea how to do this, could anyone help? :>

i've done most of the code as i said it's the rand() part giving me trouble
I think the point is to make the computer 'divide and conquer' to determine the number the user picked. In which case you shouldn't be using rand at all.
is it not divide and conquer to eliminate all wrong choices and all that is left with is the correct choice?

anyways here's the whole code, got it working after a few hours :>

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
62
63
64
65
66
67
68
69
#include <iostream>
#include <Windows.h>
#include <string>
#include <time.h>

int RandomNumber, Number, Tries, SecretNumber, HighValue ,LowValue,range;
std::string Answer;


int main()
{
	
	std::cout << "Enter a secret number" << std::endl;
	std::cin >> SecretNumber;
	system("CLS");
	srand ( time(NULL) );
	HighValue = 100;
	LowValue = 1;
	RandomNumber = rand() % HighValue + LowValue;

	while(Tries <=7)
	{
		Tries++;

	std::cout << RandomNumber << " is this your number? please tell me if it's higher or lower then your number" << std::endl;
	std::cin >> Answer;
	std::cout << "" << std::endl;
	
	if(Answer ==  "higher")

	{
		
		RandomNumber--;
		HighValue = RandomNumber;
		range =(HighValue-LowValue)+1;

		RandomNumber = LowValue+int(range*rand()/(RAND_MAX+1.0));
	
		
	}
		else if(Answer ==  "lower")
	{


		RandomNumber++;
		LowValue = RandomNumber;
	    range =(HighValue-LowValue)+1;
		RandomNumber = LowValue+int(range*rand()/(RAND_MAX+1.0));

	}



		else if(Answer ==  "correct")
	{
	std::cout <<"yay" << std::endl;
	system("pause");
	return 0;
	}




	}
	
	
	
}
Last edited on
modify the program so that no matter what number the user thinks of (1-100) the computer can guess it in 7 or less guesses.


There's no guarantee of this by randomly picking values within the possible range. It could take 100 guesses.
well crap :> forgot to think about how that :<
Divide and conquer is to choose numbers to eliminate the maximum number of possibilities. In a situation like this one, it means cutting the possibilities in half.

Example number: 48

guess 1) 50 (lower)
guess 2) 25 (higher)
guess 3) 37 (higher)
guess 4) 43 (higher)
guess 5) 46 (higher)
guess 6) 48 (correct!)
that's what i thought of aswell Disch. however is their any otherway to do this than to do have a ton of if statements?`?
I have an idea in mind, and will make this tonight. I'll post here with my solution.
@Serri

You don't need a ton of if's. Just the one's you already have. I removed the 'else' part, cause I felt they weren't really necessary. Also added in a section that checks if you cheated at the end. Removed the System("CLS") call, as a lot of programmers frown on it. Anyway, here's the newer version.

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Computer Guess Number.cpp : main project file.

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

void ClearScreen();

int RandomNumber, Number, Tries=0, SecretNumber, HighValue ,LowValue,range;
char Answer;

int main()
{

	std::cout << "Enter a secret number" << std::endl;
	std::cin >> SecretNumber;
	time_t t;
	srand((unsigned) time(&t));
	HighValue = 100;
	LowValue = 1;

	RandomNumber = rand() % 100;

	do
	{
		ClearScreen();
		std::cout << "Is " << RandomNumber << " 'H'igher or 'L'ower than your number? Or am I 'C'orrect?" << std::endl;
		Tries++;
		std::cin >> Answer;
		Answer = tolower(Answer);
		std::cout << std::endl;

		if(Answer ==  'h')
		{
			HighValue = RandomNumber-1;
			range =(HighValue-LowValue)/2;
			RandomNumber = range+LowValue;
		}
		if(Answer ==  'l')
		{
			LowValue = RandomNumber+1;
			range =(HighValue-LowValue)/2;
			RandomNumber = range+LowValue;
		}
		if(Answer ==  'c')
		{
			std::cout <<"Yay. I got in " << Tries << " guesses.." << std::endl;
			system("pause"); // There are better alternatives. Left it here for now.
			return 0;
		}
	} while(Tries < 7);

	std::cout <<"I used all of my " << Tries << " guesses.." << std::endl;
	std::cout << "You win this time. Now I'll peek at your number.." << std::endl;
	Sleep(2000);
	std::cout << "I see you picked " << SecretNumber << " as your secret number." << std::endl;
	if ( SecretNumber < LowValue || SecretNumber > HighValue)
		std::cout << "I also see that you could only beat me by CHEATING!! SHAME ON YOU!!" << std::endl;
	return 0;
}

void ClearScreen()
{
	DWORD n;
	DWORD size;
	COORD coord = {0};
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
	GetConsoleScreenBufferInfo ( h, &csbi );
	size = csbi.dwSize.X * csbi.dwSize.Y;
	FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
	GetConsoleScreenBufferInfo ( h, &csbi );
	FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );
	SetConsoleCursorPosition ( h, coord );
}
Topic archived. No new replies allowed.