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) Answers 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?

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

Joined: Jun 04, 2002
Posts: 6

posted

0

Simply add the following:

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.