The AP CS A Exam features random numbers on both the multiple choice and free response sections. Problems on the multiple choice section will use Math.random(). Student responses on the free response section should use Math.random() (not the Random class or other approaches).

In all cases, precision is required to earn the point. Solutions that generate numbers outside intended ranges, or that cannot produce all numbers within intended ranges, will usually not receive full credit.

How Math.random() works

A call to Math.random() returns a double value in the range 0.0 (inclusive) up to but not including 1.0.

The statement:

double r = Math.random();

results in r having a random value in the range:

0.0 <= r < 1.0

Math.random() on the AP CS A Exam

Generate random numbers in a given range

Both multiple choice and free response problems can require generating a random number in a precise range. On the multiple choice section, this usually involves identifying which statement does so correctly.

See Generate random numbers for a technique that allows you to check that the values generated exactly match the required range.

Determine the range, or possible values, generated

Multiple choice problems can feature expressions that include one or more calls to Math.random(). The problems often require identifying the precise range of values that could be generated.

See Determine random range for a technique. Note that the technique builds on the one from the above section.

Simulate a given probability

Both multiple choice and free response problems can require using a random number to simulate a given probability.

See Simulate probability for an easy way to do this with Math.random().

Additional Math.random() resources

Practice problems featuring Math.random()

AP Exam free response featuring Math.random()

The links below are to my solutions. Each solution has a link to the PDF with the free response questions for that year.