random number help

Hey guys! New programmer here, been teaching myself with a few different beginner/basic books. So this code/project is not for a grade nor will it be used for anything serious. Just a simple test one of the books has given me that i could use some helpful advice, hints or answers if anyone is up to it.

My task is to write a program that guesses a user's number (1-100) using a random number generator.
My approach was to first state the rules to the user saying that the computer will guess a number, then based on that number the user would respond with either h or l for higher or lower, and my program would adjust the next guess based on the users input.
my trouble comes when trying to make the random number generator change its range (from 1-100 to 1-X based on the first randnum generated)

sorry for my lengthy post, here is what i have so far
Thanks in advance!
P.S - please remember im a beginner =(
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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    srand(static_cast<unsigned int>(time(0)));
    
    int randNum = rand() % 100 + 1;
    char ready = n;
    char input;
    cout << "This program will guess your number from\n"
         << "anywhere between one and one hundred.\n\n"
         << "When the computer guesses your number please\n"
         << "respond accordingly to the computer's guess\n"
         << "H - higher (if the guess was below your number)\n"
         << "L - lower (if the guess was higher than your number\n"         
         << "Please think of your number now. . .\n\n"
         << "When you are ready type y";
    cin  >> ready
         >> endl;
         
    while (ready == y)
    {
        cout << "Computer 'I think your number is: '"
             << randNum
        cin  >> input
        
        switch (input)
        {
            case h:
                    randNum = 
                    cout << "Well then your number must surely be: "
                         << randNum
        }
        
    }


EDIT: the way i used to generate the random number was described in the book i am using, i am stuck where my code stops, what i want to do next is take the current random number, then based on user input, adjust the range of the next number to be generated, either higher or lower.
(if i wasn't clear enough before)
Last edited on
closed account (DSLq5Di1)
1
2
int randNum, lowerLimit = 0, upperLimit = 100;
randNum = lowerLimit + rand() % ((upperLimit+1) - lowerLimit); // 0-100 (inclusive) 

Given the above code, you should be able to determine the logic of your program by modifying the limits and generating a new random number based on user input.
thanks sloppy, ill give it a shot
Hello, Did you get any more working? I just did this type of program a few days ago. Are you trying to make it guess the number in 7 tries or less? Or just you're not worried about that atm?

EDIT: 7 tries or less would require just a bit more coding but its very natural actually. Play this game against yourself and see how your brain works the numbers and try to implement that in your program let me know if you need more help!
Last edited on
everything has worked great sloppy, got it complete!
for future reference could you by chance post the same function with uninclusive limits?

thanks again!
If you want exclusive, you can just add 1 to the lower limit and subtract 1 from the upper limit, couldn't you?
like this Zhuge?
randNum = lowerLimit + rand() % ((upperLimit-1) - (lowerLimit+1));

i have it like this and i often get 2 of the same answer still =/
Last edited on
Hey Phil, My primary function was doing this the whole time when a user said too high or too low:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int computerGuess(string highOrLow, int& max, int& min, int guess)
{
	if(highOrLow == "high")
	{
		max = guess;
		guess = (min+max)/2;

		return guess;
	}

	else if(highOrLow == "low")
	{
		min = guess;
		guess = (min+max)/2;

		return guess;
	}

	else
	{
		guess = (min+max)/2;

		return guess;
	}


Way i figured was - there is one formula that will work for every number to get the numbers in between.

For example if ahuman was told to guess a number between 1-100 he would start with 50, therefore (0+100)/2 = 50. That translates to the (minimum+maximum)/2 = guess - that i use. Works well :) Glad to see that you got it though

EDIT: I would recommend you break everything into functions though
Last edited on
vlad what happens when you put the & after int like that?

 
int&

i haven't learned that yet i don't think
Called a reference operator - You know how you pass a number into your functions usually? Well a reference operator makes it so that when you pass a number into a function whatever happens to that number / (Or any datatype really) Will also affect the number that you passed into it from the outside. (Because usually whatever happens in a function stays inside of the function).

In-depth look at it here: http://cplusplus.com/doc/tutorial/pointers/ just sscroll down to the reference operator
Last edited on
Topic archived. No new replies allowed.