Click to See Complete Forum and Search --> : Make the computer guess a number
PeteTheNotSoGr8
01-05-2005, 10:00 AM
I am in my Java class and am supposed make the computer guess a number that the user inputs. The computer is allowed to know (as the human was in the precursor program) wether the guess was to high or to low.(Range is 0-100)
Using the standard randomizer I am able to set new maxes with ease but the minimum value can not be set without manipulating. My teacher says an array would be required but I believe that there is an effective way to create a self adjusting minimum value.
PeteTheNotSoGr8
01-05-2005, 10:01 AM
import javax.swing.JOptionPane;
import java.util.Random;
public class CompGuess
{
public static void main(String[] args)
{
String input = JOptionPane.showInputDialog("What number do you wnat me to guess?");
int answer = Integer.parseInt(input);
int cntr=0, max =100, min=1;
Random generator = new Random();
int maybe = 1 + generator.nextInt(max);
++cntr;
if(maybe==answer)
{
System.out.println("i have guessed " + maybe + ". I have found the correct number in " + cntr + " guesses.");
}
else if(maybe<answer)
{
System.out.println("i have guessed " + maybe + ". My guess was lower than your number.");
min=maybe;
}
else
{
System.out.println("i have guessed " + maybe + ". My guess was greater than your number.");
max=maybe-1;
}
while (maybe !=answer)
{
maybe = 1 + generator.nextInt(max);
++cntr;
if(maybe==answer)
{
System.out.println("i have guessed " + maybe + ". I have found the correct number in " + cntr + " guesses.");
break;
}
else if(maybe<answer)
{
System.out.println("i have guessed " + maybe + ". My guess was lower than your number.");
min=maybe;
}
else
{
System.out.println("i have guessed " + maybe + ". My guess was greater than your number.");
max=maybe-1;
}
}
System.exit(0);
}
}
sorry forgot it in the original
tecknophreak
01-05-2005, 10:36 AM
When placing code on this form, please either use the code tags or the # button, so it's readable.
Thank you - the Code Nazi
lagitus
01-05-2005, 11:18 AM
Create a random number between 0 and (max - min) and add the result to min?
Another thing: you can make your code look much better by not repeating the if..else if..else block. You can put it all into one while statement quite easily.
edit: and you really should use indentation!
AdamZ
01-05-2005, 01:46 PM
Why use random numbers at all (do you have to)? It's better most of the time to just split the current range in half. That way no matter whether the number is above or below what the computer guesses, you'll still have half the numbers left to guess.
Or is it supposed to guess "like a person" (randomly)?
soulestream
01-05-2005, 02:33 PM
Why use random numbers at all
if you use half/mean then you could always tell how many guesses the computer was going to make at a certain number. like if you picked the number one and you were using 1-100, then it would always take 7 guesses(if my math is correct). if you use random numbers it would be just that more random.
soule
cybertron
01-05-2005, 04:25 PM
Originally posted by soulestream
if you use half/mean then you could always tell how many guesses the computer was going to make at a certain number. like if you picked the number one and you were using 1-100, then it would always take 7 guesses(if my math is correct). if you use random numbers it would be just that more random.
soule
Yeah, but why would you ever use a random search like this? Usually these random programs work the other way around, the computer picks a random number and you have to guess it. I suppose if you were writing for Windows and were afraid your code was too efficient maybe you'd do it this way:D
Doing a binary search (splitting it in half each time) on it would be the most efficient method that I know of, but if that's not the way the assignment says to do it, I guess you can't.
PeteTheNotSoGr8
01-06-2005, 09:34 AM
thanx guys
I did what you suggested about max min and worked great thanks
as for the binary search that would possibly work. But other people in my class have enetered infinite loops with ease wuite regularly despite that it should not have so I am little hesitant to attempt that.
I also would have used the code tags but this is the first time I have posted code and didn't see them and since I was in class I had to do it quickly so the teacher wouldn't throw a hissy-fit.
bryan.6
01-06-2005, 12:28 PM
Originally posted by PeteTheNotSoGr8
as for the binary search that would possibly work. But other people in my class have enetered infinite loops with ease wuite regularly despite that it should not have so I am little hesitant to attempt that.
but you're smarter than the other people in your class! give it a try! if you fail, no big deal, 'cause you have this one working; if you succeed, then you've got something to beam about!
i feel like a father saying that
Sepero
01-06-2005, 03:45 PM
Originally posted by PeteTheNotSoGr8
as for the binary search that would possibly work. But other people in my class have enetered infinite loops with ease quite regularly despite that it should not have so I am little hesitant to attempt that.If they entered an infinite loop, I would be willing to bet they are 1 digit off in their math. Probably confusion with fractions>
100/2 = 50
50/2 = 25
25/2 = 12.5 not an integer
Arjay
01-06-2005, 04:00 PM
If they entered an infinite loop, I would be willing to bet they are 1 digit off in their math. Probably confusion with fractions>
100/2 = 50
50/2 = 25
25/2 = 12.5 not an integer
Doesn't a binary search take the next whole number, for example 13? Or maybe it just divides 24/2. I could be wrong, please correct me if i am.
Cheers
bryan.6
01-06-2005, 07:34 PM
in java, the program would most certainly do integer division, unless if the variable you were putting it in is a double or float. so 25/2 = 12.
PeteTheNotSoGr8
01-07-2005, 09:31 AM
thanx for the encouragement guys. But I feel that my program is already better than 75% of the class's. As for being smarter than the guys who entered the magical place of infinite loops they are all better with computers than I am. Besides I need to get working pn the next project, x number of circles with random radius lengths, and in random positions, oh what fun ;) .
Thanx again.