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 question re Math.round() and random()

Greenhorn
Posts: 29
• Number of slices to send:
Optional 'thank-you' note:
This is a question from the RHE final exam:

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

The correct answer is B)3 and here's the explanation:

Math.random() returns a double greater than or equal to 0.0 and less than 1.0. Math.random() + 2.50001 is a double greater than 2.5 and less than 3.5. Math.round() of any number between but not including 2.5 and 3.5 is 3.

Couldn't Math.random() return, say, .999999, in which case 2.50001 + .999999 = 3.500009. And wouldn't that round up to 4? What am I getting wrong?
Thanks, Sylvia

Ranch Hand
Posts: 281
• Number of slices to send:
Optional 'thank-you' note:
Hmmm...that's really weird. I'll be interested to hear a better explanation because I agree with you.

Sheriff
Posts: 9109
12
• Number of slices to send:
Optional 'thank-you' note:
Check the on-line errata for the book.

Bartender
Posts: 2205
• Number of slices to send:
Optional 'thank-you' note:

Corrected in the 2nd printing: The answer for Question #27 is in error. The correct answer is C, and the explanation is: Math.random() returns a number greater than or equal to zero and strictly less than one. If the value is greater than 0.99999, the full expression rounds to 4; otherwise the expression rounds to 3.

Good thinking sylvia! You are correct.

Author & Gold Digger
Posts: 7617
6
• Number of slices to send:
Optional 'thank-you' note:
Moving this to mock exam errata forum.

High Plains Drifter
Posts: 7289
• Number of slices to send:
Optional 'thank-you' note:
As a side-note, I lost the battle on this particular question to have the answers re-written. The "correct answer" (It is impossible to say) is at least as bad as saying the answer must be 3; it is, after all, pre-determined what the range of possible answers are: it's either 3 or 4, and there's Nothing impossible about knowing that.
That's what happens when you get editors who don't know programming, though. Oh well, seems like enough people are happy with C) that it will stand.

Wanderer
Posts: 18671
• Number of slices to send:
Optional 'thank-you' note:
Maybe you should have tried to get them to replace Math.random() with 0.99 * Math.random() instead. Ah, well...

Michael Ernest
High Plains Drifter
Posts: 7289
• Number of slices to send:
Optional 'thank-you' note:
Doh! And I usually pride myself on finding the workaround that least taxes the editors. Darn, that's a good one...

Leverager of our synergies
Posts: 10065
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Michael Ernest:
The "correct answer" (It is impossible to say) is at least as bad as saying the answer must be 3; it is, after all, pre-determined what the range of possible answers are: it's either 3 or 4, and there's Nothing impossible about knowing that.

Actually, "it is impossible to say" statement is valid for Java2K language.
"Java2K is not a deterministic language, but a probabilistic one. Every builtin function has two different implementations, one of which is randomly chosen by the sandbox at runtime."

Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
• Number of slices to send:
Optional 'thank-you' note:

Java2K language...
An esoteric programming language is a programming language deliberately designed to be unusable.

What a waste of brain potential... What benefit does an esoteric language have?

 Consider Paul's rocket mass heater.