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.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
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 ofx(its distance from zero, always non-negative).Math.abs(-7)is7; the return type matches the argument (intfor anintargument,doublefor adouble).Math.pow(base, exp)returnsbaseraised to the powerexp, as adouble.Math.pow(2, 3)is8.0(note the decimal, because the result is adouble).Math.sqrt(x)returns the square root ofx, as adouble.Math.sqrt(25)is5.0.Math.random()returns adoublein the half-open range[0.0, 1.0): it can return0.0but never1.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, adouble(becauseMath.powalways returns adouble).
Q2. Write an expression that produces a random integer from 5 to 15 inclusive. [2 points]
- Cue.
(int) (Math.random() * 11) + 5, because15 - 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
- Topic 2.8 Wrapper Classes: Integer and Double: use the Integer and Double wrapper classes, including autoboxing and unboxing, the MIN_VALUE and MAX_VALUE constants, and parsing methods.
A focused answer to AP CSA Topic 2.8, covering why wrapper classes exist, creating Integer and Double objects, autoboxing and unboxing, Integer.MIN_VALUE and MAX_VALUE, parseInt and parseDouble, and the == versus equals trap for wrappers, with a worked trace.
- Topic 1.5 Casting and Ranges of Variables: use casting to convert between int and double, predict the effect of truncation, and recognize that an int has a finite range that can overflow.
A focused answer to AP CSA Topic 1.5, covering explicit casting between int and double, the truncation that casting to int causes, where the cast applies in an expression, the finite range of an int, and integer overflow, with a traced worked example.
- Topic 2.5 Calling a Non-void Method: call a method that returns a value, and use the returned value by storing it, printing it, or including it in an expression.
A focused answer to AP CSA Topic 2.5, covering methods that return a value, the return type, using a returned value in an assignment, expression or print, the difference from a void method, and chaining method calls, with a worked trace.
- Topic 1.3 Expressions and Assignment Statements: evaluate arithmetic expressions using operator precedence, integer division and the modulo operator, and assign results to variables.
A focused answer to AP CSA Topic 1.3, covering arithmetic operators, operator precedence, integer division truncation, the modulo operator, and how assignment statements store results, with a fully traced worked evaluation.
- Topic 2.7 String Methods: call the String methods in the AP Java subset (length, substring, indexOf, equals, compareTo), respecting zero-based indexing and the immutability of String objects.
A focused answer to AP CSA Topic 2.7, covering the required String methods length, substring (both forms), indexOf, equals and compareTo, zero-based indexing, the half-open range of substring, why == differs from equals, and String immutability, with a worked trace.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)