Hello,

if i want the computer to guess a number between 1 - 100

i'll do in java

``````int min = 1;
int max = 100;``````

I have already create a random number for the computer.

so the first random number the computer will choose is 49

as

``````in cGuess
cGuess = max - min / 2

----

cGuess = 100 - 1 / 2 = 49``````

what I want here is how I make the computer choose max = 49 not 100, if the guessed number was wrong.

and every time will take the new cGuess as max number until it guess the random number.

I think there is loop for it, but I tried everything but i couldn't solve it.

please help

thanks.

## Recommended Answers

try using the different Math funcions that are implemented into java

``````Math.round();
Math.random();``````

There are multiple ways to get the computer to find a number between 1 and 100. Try using a third var to increase or decrease the computers guess until it matches the number and use a couple of if statements to determine if it needs to increase or decrease it …

## All 8 Replies

try using the different Math funcions that are implemented into java

``````Math.round();
Math.random();``````

There are multiple ways to get the computer to find a number between 1 and 100. Try using a third var to increase or decrease the computers guess until it matches the number and use a couple of if statements to determine if it needs to increase or decrease it and to check if the guess is equal to the number.

You need to make var decrease by an amount(doesn't need to be half) each time the computer changes its guess so that the guess will eventually reach the number.

To increase the guess

``````guess = guess + var
var = var/2``````

to decrease it

``````guess = guess - var
var = var/2``````

See if you can figure it out from here if not I will give you another hint.

Also to get a random integer between 1 and 100 use

``1 + (int)(Math.random() * 99)``

hope this helps
if anything in this post does not make sense please say so and I or someone else will explain it.

I think that the student is trying to implement a binary search. It seems that the user has selected a number in the range [1..100] and the computer must guess it. I assume that after each guess, the user tells the computer if the guess was less than, equal to, or greater than the number the user has in mind.

It can be helpful to realize that in Java, this code

``int cGuess = max - min / 2;``

is equivalent to this code

``int cGuess = max - (min / 2);``

and that this is probably not what you want.
This is probably closer:

``int cGuess = (max - min) / 2;``

(It's probably still not what you want. But I'll let you figure out how to fix it. ;-)

Something else to think about: If the computer guesses 49 and finds that the user's number is less than this, should the computer set "max" to 49, or would some other value be slightly better?

Thanks for the reply.

I figured out some of it.

but I still have a problem. so If i have the random number for the computer to be 0.

``````cGuess = cGuess - var;
var = var / 2;``````

var = max - min. which is 100 - 1 = 99

var = 99 / 2 = 49

its going to end like 16 or -16 for the computer guess, and will not reach 0.

Am i going right, or did i mess up the thing :D?

"if i want the computer to guess a number between 1 - 100" [from first post]

"but I still have a problem. so If i have the random number for the computer to be 0." ... "its going to end like 16 or -16 for the computer guess, and will not reach 0."

If you start with the assumption that the number should be in the range from 1 to 100, inclusive, then using the value zero would be cheating.

^ that didn't help but thx for the reply.

Suppose that you know that the number is between 46 and 52, inclusive.

Suppose that you make the best possible guess, which is 49, and you find that the number is less than 49. So now you know that the number is between 46 and 48.

So...
48 - 46 is 2
and
2 / 2 is 1
and
"1" is...
Not the best number to use for your next guess. :-O

I think that we don't quite have the math right yet. We need math that when given "46 to 48" will give us "47".

Please explain how you want your search to work more carefully than you did in your first post as me and JeffGrigg both have different ideas as to what you meant. What I thought you meant is that you want the computer to guess any number between 1 and 100 starting with no information and as it makes guesses increases or decreases the value until it reaches a match.

its going to end like 16 or -16 for the computer guess, and will not reach 0.

The code pieces I gave you to be put in a loop that would repeat itself until the numbers matched. :icon_wink: Var need to decrease every time you make a new guess as each guess will be closer to the number than the last.

Here are a couple runs of a program that I think does pretty close to what you want. If it does not do what you want please tell me. This program Prints the variable values every time it makes a guess so you can see what it is doing.

``````RUN 1:
Guess: 50
var is: 25
Guess: 75
new var is: 12
Guess: 63
new var is: 6
Guess: 57
new var is: 3
Guess: 54
new var is: 1
The number is 54

RUN 2:
Guess: 50
var is: 25
Guess: 75
new var is: 12
Guess: 87
new var is: 6
The number is 87

RUN 3:
Guess: 50
var is: 25
Guess: 75
new var is: 12
Guess: 63
new var is: 6
Guess: 69
new var is: 3
Guess: 66
new var is: 1
Guess: 67
new var is: 1
Guess: 68
new var is: 1
The number is 68

RUN 4:
Guess: 50
var is: 25
Guess: 25
new var is: 12
Guess: 13
new var is: 6
Guess: 7
new var is: 3
Guess: 10
new var is: 1
The number is 10

RUN 5:
Guess: 50
var is: 25
Guess: 25
new var is: 12
Guess: 13
new var is: 6
Guess: 7
new var is: 3
The number is 7``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.