aspose file tools*
The moose likes Beginning Java and the fly likes Help with understanding math.random() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with understanding math.random()" Watch "Help with understanding math.random()" New topic
Author

Help with understanding math.random()

Shawn Houston
Greenhorn

Joined: Oct 14, 2004
Posts: 22
I have a program here that requires a user to enter a guess from 1-100 and then tells the user whether he/she went to high or to low. I'm very new to JAVA and trying to figure all this stuff out..heres a copy of the program.
The program comes up with no compiler errors, but when i enter the guess the program freezes on me and doesnt go with the loop..any suggestions?

import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;
public class guess
{
static BufferedReader keyboard =
new BufferedReader(new InputStreamReader(System.in));

public static void main(String[] args) throws IOException
{
int guess;
int randomnumber;
int number;
int count = 0;

randomnumber = (int)( Math.random() * 100 + 1);
guess = randomnumber;

System.out.print("Please enter in your guess: ");
System.out.flush();
number = Integer.parseInt(keyboard.readLine());
System.out.println();

while(number != guess);
{
count = count + 1;

if(number > guess)
System.out.print("Your number was too large, try again");
else
System.out.print("Your number was too small, try again");

System.out.print("Please enter in your guess: ");
System.out.flush();
number = Integer.parseInt(keyboard.readLine());
System.out.println();
}
System.out.println("It took you " + count + "guesses");
}


}
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

The problem is the semicolon before the body of your while loop:
while(number != guess);

If you remove that semicolon (and start count at 1 rather than 0), the program seems to work fine...

[ October 14, 2004: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Shawn Houston
Greenhorn

Joined: Oct 14, 2004
Posts: 22
Thank you much,
Sometimes I seem to overthink the situation with programs. It's good to see that I had the whole random number thing correct, Couldnt find anything on it in the book I have. I have a handful of other programs with while and do..while loops within them that I had the semicolon there at the end, which put a hault on them aswell.
Thank you again,
Shawn
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Yes, your use of Math.random() is correct!

There are 2 key points to this approach...

First key point: Math.random() generates a double between 0.0 and 1.0. Technically, this interval includes 0.0, but not 1.0. (I say "technically" because the chances of actually getting 0.0 is... Well, rather low.)

So multiplying the double Math.random() by 100 then gives a double between 0.0 and 100.0 -- including 0.0, but not 100.0. Adding 1 then gives a double between 1.0 and 101.0 (not including 101.0).

Second key point: Casting a floating-point primitive to a integral primitive always truncates (note that an explicit cast is always needed in these cases, even if going from a 32-bit float to a 64-bit long). So casting the above double to an int will give an int between 1 and 100 inclusively (since the double could be up to -- but not actually including -- 101.0).
[ October 15, 2004: Message edited by: marc weber ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help with understanding math.random()