Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

random function

 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic