Page 1354 - AP Computer Science A, 7th edition
P. 1354

36. (D) The statement
double value = Math.random();
generates a random double in the range 0 ≤ value < 1. Since random doubles are uniformly distributed in this interval, 45 percent of the time you can expect value to be in the range 0 ≤ value < 0.45. Therefore, a test for value in this range can be a test for whether the serve of a WeakPlayer went in. Since Math.random() never returns a negative number, the test in implementation II, value < 0.45, is sufficient. The test in implementation I would be correct if || were changed to && (“or” changed to “and”—both parts must be true). Implementation III also works. The expression
(int) (Math.random() ∗ 100)
returns a random integer from 0 to 99, each equally likely. Thus, 45 percent of the time, the integer val will be in the range 0 ≤ val ≤ 44. Therefore, a test for val in this range can be used to test whether the serve was in.






























































































   1352   1353   1354   1355   1356