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
• Ron McLeod
• Rob Spoor
• Tim Cooke
• Junilu Lacar
Sheriffs:
• Henry Wong
• Liutauras Vilda
• Jeanne Boyarsky
Saloon Keepers:
• Jesse Silverman
• Tim Holloway
• Stephan van Hulst
• Tim Moores
• Carey Brown
Bartenders:
• Al Hobbs
• Mikalai Zaikin
• Piet Souris

# Q: Math.round(Math.random() + 2.50001; ????

Ranch Hand
Posts: 158
• • Number of slices to send:
Optional 'thank-you' note:
• • Here's a question from the Programmer's Final Exam in the Simon Roberts "Complete Java2 Certification Study Guide"

What is the value of the following expression?
Math.round(Math.random() + 2.50001);
A. 2
B. 3
C. It is impossible to say.

I say "C" but the book says B ???
------------------------------------------------
I set up a program to test this ... one of the random numbers I got was 0.9587593517384946
so I figure I could eventually come up with a number like 0.9999993517384946
so if I add 0.9999993517384946 + 2.50001
I get something like 3.50000035
---------------------------------------------
now the documentation for

public static long round(double a)
Returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of
the result, and casting the result to type long. In other words, the result is equal to the value of the expression:
(long)Math.floor(a + 0.5d)

so (long)Math.floor(3.50000035 + 0.5d)
== (long)Math.floor(3.50000035 + 0.5d)
== (long)Math.floor(4.00000035)
== 4.
floor - Returns: the largest (closest to positive infinity) floating-point value that is not greater than the argument and is
equal to a mathematical integer.

[B] I'd say most of the time you will get 3 as the answer EXCEPT when the random(0 number is greater than .999999...

based on this possibility, how can we say other than "impossible to say" as the right choice?

Ranch Hand
Posts: 1865
• • Number of slices to send:
Optional 'thank-you' note:
• • David,
You are correct. The result could be 3.0 or 4.0. To prove to yourself that 4.0 is possible then just run the following.

david eberhardt
Ranch Hand
Posts: 158
• • Number of slices to send:
Optional 'thank-you' note:
• • Dan,
thanks.
I had set up a small loop to see if and when I got something equal to or bigger than 0.999999 (that's when the result will end up as a 4) .... didn't happen as I made the loop run only 100 times.
Usually, the random number is less than that.
I might just set up a loop to run 10,000 times and test for that number ... I imagine I will eventually see one!
Anyways - I guess that sometimes book writers are pressed for time and miss one now and then.
Here's some code I wrote to show how I came up with my answer "impossible to say" since choosing 3 is not correct for all possible values that math.random() could generate:

david eberhardt
Ranch Hand
Posts: 158
• • Number of slices to send:
Optional 'thank-you' note:
• • some of the output from my test program:
C:\BIN>java OutThing
long i1 = Math.round(0.999999 + 2.50001);
i1 = 4
long i2 = Math.round(Math.random() + 2.50001);

0.052595364524934185 3
0.7981981280117669 3
0.3908890857231241 3
[ November 30, 2002: Message edited by: david eberhardt ] Don't get me started about those stupid light bulbs.