The AP CS A Exam multiple choice and free response sections can both feature questions that require the use of random numbers to simulate probabilities. The approach below is easy and common.
The statement
double r = Math.random();
results in r having a random value in the range
0.0 <= r < 1.0
The value return by Math.random() is evenly distributed within that range. (Any value within the range is equally likely.)
Using Math.random() to simulate a given probability does not require manipulating the output. Instead, we simply check if the result is less than the desired probability.
Example 1
Print "player wins" 35% of the time.
if(Math.random() < 0.35)
System.out.println("player wins");
Example 2
Print "yes" 15% of the time, "no" 35% of the time, and "maybe" the other 50% of the time.
double r = Math.random();
if(r < 0.15)
System.out.println("yes");
else if(r < 0.5)
System.out.println("no");
else
System.out.println("maybe");
We generate only one random value, not one random value per possibility. We want the else if condition to be true 35% of the time. 0.15 + 0.35 is 0.5.