Please give hint about number generation?

I'm doing the beginner exercises on this website, and I'm having trouble with the "Bracketing Search."

★★ Modify the program so that instead of the user guessing a number the computer came up with, the computer guesses the number that the user has secretely decided. The user must tell the computer whether it guesed too high or too low.

I'm tweaking the code so instead of guessing just once, the computer keeps on guessing over and over until it finally guesses my number.

Why is it that my computer keeps using the same random number generated initially, instead of replacing the generated # with a new random #?

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
#include <iostream>
#include <ctime>
#include <limits>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>


using namespace std;

int randomness()
{
    int j;
    j = rand();
}

int main()
{
    double inf = numeric_limits<double>::infinity();

    srand((unsigned)time(NULL));

    int x;
    int y = rand();
    int z;

    cout << " " << endl;

    cout << "The user must enter a random # here: " << endl;
    cin >> x;

    while (y < x)
    {
        cout << "Computer guessed " << y << " ." << endl;
        cout << "Mr.Computer guessed too low... try again, Computer." << endl;
        int y = 0;

        for (z = 0; z < 2; z++) //This loop is (theorically) supposed to generate one new random number
        {
            y = randomness(); // This is supposed to replace the previous random number with a NEW random number
            break;
        }

    }

    while (y > x)
    {
        
        cout << "Computer guessed " << y << " ." << endl;
        cout << "Mr Computer guessed too high... try again, Computer." << endl;
 
        int y = 0;

        for (z = 0; z < 2; z++)
        {
            y = randomness();
            break;
        }
    }

    if (y == x)
    {
        cout << "Computer guessed it right!" << endl;
    }

    return 0;
}


Just to visualize my problem, this is an example of an output:

1
2
3
4
5
6
7
8
9
10
11
The user must enter a random # here:
55966

Computer guessed 7825.
"Mr.Computer guessed too low... try again, Computer."
Computer guessed 7825.
"Mr.Computer guessed too low... try again, Computer."
Computer guessed 7825.
"Mr.Computer guessed too low... try again, Computer."

.....

The randomness() does not have a return statement so the value it returns is undefined.
Also, the computer should use the fact that the previous guess was too high or too low when it makes its next guess. To do that effectively, you need to restructure the code to looks something like this:
set up variables with the high and low bounds of the number
ask user to pick a number between the bounds
have computer make a guess between high and low
1
2
3
4
5
6
7
8
9
while (guess != users's number) {
    if (guess is too high) set high bound to the guess
    else if (guess is too low) set low bound to the guess
    else {
        guess is right
         break;
    }
    make another guess between high and low bound.
} 

Note that in keeping with good practice and the Standard library, you should define your bounds so that low <= number < high. In other words, the low bound could be the number, but the high bound is one more than the highest possible number.
Topic archived. No new replies allowed.