aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q: Math.round(Math.random() + 2.50001;  ???? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q: Math.round(Math.random() + 2.50001;  ????" Watch "Q: Math.round(Math.random() + 2.50001;  ????" New topic
Author

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

david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
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?
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
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

Joined: Jul 02, 2002
Posts: 158
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Q: Math.round(Math.random() + 2.50001; ????
 
Similar Threads
Math Round
RHE Final #27
RHE final exam question
Math.random
RHE question re Math.round() and random()