This week's book giveaway is in the Other Languages forum.We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Error in "final" test on cd for RHE book

Charles Earwood
Greenhorn
Posts: 6
I found an error with question #27 on a test in the RHE book's cd. The name of the test is "final".
The problem is below:
Math.round(Math.random() + 2.50001)
A. 2
B. 3
C. It is impossible to say.

RHE says that the answer is B. I chose C. I wrote a simple program below to test this out and had the output that it could be either 3 or 4. This confirmed my suspicion. What do yo think?

Anthony Villanueva
Ranch Hand
Posts: 1055
The answer should be B. 3. Math.random() always returns a value x where 0 <= x < 1, so the argument of Math.round() is always strictly greater than 2.5 which means it always returns 3.
I found your code difficult to decipher so I wrote my own. I got ten 3's outputted to the screen several times.

[ June 21, 2002: Message edited by: Anthony Villanueva ]

Charles Earwood
Greenhorn
Posts: 6

2.50001 + .9999999 = 3.5000099
The reason you got 3's is because most of the time the return value of .random() is less than .99999 . In my code, I filter out all values that are less than the value necessary to get a 4.

Jose Botella
Ranch Hand
Posts: 2120
to Anthony,
However imagine random returns 0.99999 or anything bigger. The result is 4.
[ June 21, 2002: Message edited by: Jose Botella ]

Anthony Villanueva
Ranch Hand
Posts: 1055
Whoooops! (so much for buying those lottery tickets....)

Jessica Sant
Sheriff
Posts: 4313
moving this to Mock Exam Errata now that it's been confirmed as a goof...