Skip to main content
United StatesComputer ScienceSyllabus dot point

Which Math class methods does AP CSA require, and how do you generate a random number in a chosen range?

Topic 2.9 Using the Math Class: call the static Math methods in the AP Java subset (abs, pow, sqrt, random) and generate a random integer or double in a specified range.

A focused answer to AP CSA Topic 2.9, covering the required static Math methods abs, pow, sqrt and random, why Math methods are called on the class, the return types, and the standard formula for generating a random int in a range, with a worked trace.

Generated by Claude Opus 4.810 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. Math methods are static
  3. The required methods
  4. Generating a random number in a range
  5. Try this

What this topic is asking

The College Board (Topic 2.9) wants you to call the static Math methods in the AP Java subset - Math.abs, Math.pow, Math.sqrt and Math.random - on the class name, to know their return types, and to apply the standard formula to generate a random integer in a specified range. The random-number formula is one of the most reliably tested ideas in Unit 2.

Math methods are static

The required methods

  • Math.abs(x) returns the absolute value of x (its distance from zero, always non-negative). Math.abs(-7) is 7; the return type matches the argument (int for an int argument, double for a double).
  • Math.pow(base, exp) returns base raised to the power exp, as a double. Math.pow(2, 3) is 8.0 (note the decimal, because the result is a double).
  • Math.sqrt(x) returns the square root of x, as a double. Math.sqrt(25) is 5.0.
  • Math.random() returns a double in the half-open range [0.0, 1.0): it can return 0.0 but never 1.0.

Because pow and sqrt return a double, store their results in a double (or cast if you genuinely want an int).

Generating a random number in a range

The standard formula is:

int value = (int) (Math.random() * (max - min + 1)) + min;

Work it through for a die (1 to 6): max - min + 1 is 6, so Math.random() * 6 is in [0.0, 6.0); casting truncates to 0 through 5; adding min (1) gives 1 through 6. The order is critical: multiply, then cast, then add. Casting Math.random() to int first always gives 0.

Try this

Q1. State the value and type of Math.pow(3, 2). [2 points]

  • Cue. 9.0, a double (because Math.pow always returns a double).

Q2. Write an expression that produces a random integer from 5 to 15 inclusive. [2 points]

  • Cue. (int) (Math.random() * 11) + 5, because 15 - 5 + 1 = 11.

Exam-style practice questions

Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

AP 2021 (style)1 marksMultiple choice. Which expression generates a random integer from 1 to 6 inclusive (like a die roll)? (A) `(int) (Math.random() * 6)` (B) `(int) (Math.random() * 6) + 1` (C) `(int) (Math.random() * 7) + 1` (D) `Math.random() * 6 + 1` (E) `(int) Math.random() * 6 + 1`
Show worked answer →

The answer is (B).

Math.random() returns a double in [0.0, 1.0). Multiplying by 6 gives [0.0, 6.0); casting to int truncates to one of 0, 1, 2, 3, 4, 5; adding 1 shifts to 1 through 6. (A) gives 0 to 5. (C) gives 1 to 7. (D) is not an int and can exceed 6. (E) casts Math.random() to int first, which is always 0, so it always gives 1. The pattern is (int) (Math.random() * range) + min, where range is the count of values.

Markers reward the formula (int)(Math.random() * (max - min + 1)) + min and casting after multiplying.

AP 2020 (style)4 marksFree response (code writing). Write a code segment that stores in a `double` variable `dist` the distance from the origin to the point (`x`, `y`), where `x` and `y` are given `double` variables, using the formula distance equals the square root of x squared plus y squared. Use Math class methods. Then store in an `int` variable `roll` a random integer from 1 to 10 inclusive.
Show worked answer →

A 4-point question testing Math.sqrt, Math.pow and Math.random.

double dist = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
int roll = (int) (Math.random() * 10) + 1;

Point 1: Math.pow(x, 2) and Math.pow(y, 2) (or x * x and y * y) for the squares. Point 2: Math.sqrt(...) of their sum for the distance. Point 3: Math.random() * 10 then cast to int for 0 to 9. Point 4: adding 1 to shift the range to 1 through 10. Casting before multiplying, or using the wrong multiplier, loses marks.

Related dot points

Sources & how we know this