This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes random function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "random function" Watch "random function" New topic
Author

random function

Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
double x = Math.random()*9;
return (int)x;
WHy am I getting a possible loss of precison error? What are my choices?
Tom Ben
Ranch Hand

Joined: Aug 17, 2001
Posts: 109
I tried this and never got an error. Can you display the rest of the code in how you are using it?
This is how I am using it
public static void main(String args[]) {
System.out.println(ran());
}
static int ran() {
double x = Math.random()*9;
return (int)x;
}
Hope this maybe helps.
Originally posted by Anthony Smith:
double x = Math.random()*9;
return (int)x;
WHy am I getting a possible loss of precison error? What are my choices?


------------------
Sun Certified Programmer on the Java 2 Platform


Sun Certified Programmer on the Java 2 Platform
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: random function