Skip to main content

Unit 1 · Topic 1.11

1.11 Math Class

The Math class gives you absolute value, powers, square roots and random numbers. The random-number formulas are the part most students get wrong, so this topic spends extra time on building a range with the right endpoints.

Key terms

  • Math class
  • Math.abs
  • Math.pow
  • Math.sqrt
  • Math.random
  • inclusive vs. exclusive

The Math class

Math is in the java.lang package, so it's always available with no import. Every method in it is a class method (static), so you call each one with Math. in front. You need these five, all listed on the Java Quick Reference:

MethodReturnsExample
Math.abs(int x)the absolute value, as an intMath.abs(-7) is 7
Math.abs(double x)the absolute value, as a doubleMath.abs(-2.5) is 2.5
Math.pow(double base, double exp)base raised to the power exp, as a doubleMath.pow(2, 3) is 8.0
Math.sqrt(double x)the non-negative square root, as a doubleMath.sqrt(25) is 5.0
Math.random()a random double from 0.0 up to but not including 1.0something like 0.7254

Watch the return types

pow and sqrt always return a double, even when the answer is a whole number. Math.pow(2, 3) is 8.0, so int p = Math.pow(2, 3); won't compile. Cast it: int p = (int) Math.pow(2, 3);.

You can combine the methods. The distance between two points uses all of them:

int x1 = 1; int y1 = 2; int x2 = 4; int y2 = 6; double d = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); System.out.println(d);

This prints 5.0: the differences are 3 and 4, and the square root of 9 + 16 is 5.

Random numbers in a range

Math.random() returns a value greater than or equal to 0.0 and less than 1.0. Including an endpoint is called inclusive; leaving it out is exclusive. So the range is 0.0 inclusive to 1.0 exclusive.

To get a random int, stretch the range by multiplying, cut off the decimal with (int), then shift it by adding:

int roll = (int) (Math.random() * 6) + 1;

Math.random() * 6 is from 0.0 up to (not including) 6.0. Casting gives one of 0, 1, 2, 3, 4 or 5. Adding 1 gives 1 to 6, like a die.

  • For a random int from min to max, both inclusive: (int) (Math.random() * (max - min + 1)) + min.
  • The number you multiply by is how many different values you want. The number you add is the smallest value.
  • For a random double from a (inclusive) to b (exclusive): Math.random() * (b - a) + a. For example, Math.random() * 5 + 20 gives a value from 20.0 up to but not including 25.0.

Where the parentheses go

The cast must cover the whole product. (int) Math.random() * 6 casts Math.random() alone, which is always 0, so the whole expression is always 0. Write (int) (Math.random() * 6).

Worked examples

Try each one yourself first, then open the solution.

  1. Example 1

    Reading a random range

    What values can value hold after this statement runs?int value = (int) (Math.random() * 8) + 3;

    Show the solution
    1. Step 1: Math.random() is at least 0.0 and less than 1.0.
    2. Step 2: Multiplying by 8 gives at least 0.0 and less than 8.0.
    3. Step 3: Casting truncates, so the result is a whole number from 0 to 7. It can never be 8, because the product never reaches 8.0.
    4. Step 4: Adding 3 shifts that to 3 through 10.
    5. Step 5: Check with the formula: 8 values starting at 3 means 3, 4, 5, 6, 7, 8, 9 and 10.

    Answer: Any integer from 3 to 10, inclusive: 3, 4, 5, 6, 7, 8, 9 or 10.

  2. Example 2

    Writing a random range

    Write an expression for a random integer from 10 to 20, inclusive.

    Show the solution
    1. Step 1: Count the values: 20 − 10 + 1 = 11. That's the number to multiply by.
    2. Step 2: The smallest value is 10. That's the number to add.
    3. Step 3: Put the cast around the product: (int) (Math.random() * 11) + 10.
    4. Step 4: Check the ends: the smallest product cast is 0, giving 10; the largest is 10, giving 20.

    Answer: (int) (Math.random() * 11) + 10

Common mistakes

  • Multiplying by max - min instead of max - min + 1, which makes the top value impossible.
  • Casting only Math.random(), as in (int) Math.random() * 6. That's always 0.
  • Storing the result of Math.pow or Math.sqrt in an int without a cast.
  • Thinking Math.random() can return 1.0. It can't; the top end is exclusive.

On the exam

  • Expect questions that show a Math.random() expression and ask for its possible values, or ask which expression produces a given range. Work out the smallest and largest possible results.
  • The Quick Reference only lists abs, pow, sqrt and random. Don't rely on other Math methods in free-response answers; write your own comparison or use a cast instead.

Connected topics

Videos

  • AP Computer Science A - Topic 1.11 - Part 1: Math Class

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • AP CSA Using Objects and Methods – Math Class

    Goldie's Math EmporiumWatch on YouTube (opens in a new tab)

  • Random Numbers With Math.random() (Java Tutorial)

    Bill BarnumWatch on YouTube (opens in a new tab)

  • AP Computer Science A - Topic 1.11 - Part 2: Math Class

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • Java Math class 📐【6 minutes】

    Bro CodeWatch on YouTube (opens in a new tab)

Check yourself

4 questions on 1.11 Math Class. Pick an answer to see if you got it, and why.

Question 1 of 4

Which of the following expressions evaluates to a random integer from 5 to 12, inclusive, with each value equally likely?

Question 2 of 4

Consider the following code segment.System.out.println(Math.pow(2, 3));What is printed as a result of executing the code segment?

Question 3 of 4

Consider the following code segment.double r = Math.sqrt(Math.abs(-16)) + Math.abs(3 - 7); System.out.println(r);What is printed as a result of executing the code segment?

Question 4 of 4

Which of the following statements compiles without error?

0 of 4 answered