• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

RHE, Math.random() and Math.round()

 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the RHE the question (27, from the final test) about what will be the value of
Math.round(Math.random() + 2.50001);
is given to be 3.
But I thing the (Math.random() + 2.50001) is < 3.50001, that means
it can be 3.5000009, and this number rounded will be 4.
I tried the code:
public class X
{
public static void main(String args[])
{
for(int i= 0;;i++) {
double d = Math.round(Math.random() + 2.50001);
// System.out.println("d is: " + d);
if(d == 4.0) {
System.out.println("I had right...." + d + "I made:" + i + "attempts");
break;
}
}
}
}
which after some tens of thounsands of attempts gives 4.
Is it so that the answer will be "impossible to say" or
something like that?

Thanks,
Cristian
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cristian -
This problem has some unfortunate wording. Of course it is completely possible to predict the range of answers; the available answers just don't include '4'.
This should be corrected on the errata sheet for the book at www.sybex.com. Our apologies for the error.
Regards,
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic