The Stacks
UNIT 1: USING OBJECTS AND METHODS · TOPIC 1.11

1.11 Math Class

Five Math methods are on the reference sheet. Math.random is the one with a formula you must memorize.

What you need to know

  • Math.abs(x) — absolute value; returns int for int input, double for double.
  • Math.pow(base, exp) — base raised to exp; always returns a double, even Math.pow(2, 3) is 8.0.
  • Math.sqrt(x) — square root, returns a double.
  • Math.random() — returns a double ≥ 0.0 and < 1.0. Never exactly 1.0.
  • Random int in a range [low, high] inclusive: (int) (Math.random() * (high - low + 1)) + low. Number of possible values = high − low + 1.
  • Example: die roll 1–6 is (int) (Math.random() * 6) + 1. Random 10–20: (int) (Math.random() * 11) + 10.
  • All Math methods are static: always Math.method(...), never new Math().

Worked example

int a = Math.abs(-7);                          // 7
double b = Math.pow(2, 10);                     // 1024.0
double c = Math.sqrt(2.25);                     // 1.5
int roll = (int) (Math.random() * 6) + 1;       // 1..6
int r = (int) (Math.random() * 50) + 25;        // 25..74 (50 values)
int wrong = (int) Math.random() * 6 + 1;        // always 1! cast binds first

The last line is the classic error: (int) Math.random() is 0 before the multiplication happens.

Exam tip: For "which expression produces a random integer from a to b," check two things: the multiplier equals the count (b − a + 1), and the added value is a. For "what range does this produce," the minimum is the added value and the maximum is that plus the multiplier minus one.

Going deeper

The nuance, edge cases, and connections that turn a 3 into a 5.

  • Math.abs is overloaded: abs(int) returns int, abs(double) returns double. Math.abs(-3) is 3; Math.abs(-3.0) is 3.0.
  • Math.pow takes two doubles (ints widen) and always returns a double. Math.pow(2, 3) is 8.0. Assigning it to an int requires a cast: int x = (int) Math.pow(2, 3);.
  • Math.sqrt returns a double. Negative input gives NaN, not an exception.
  • Math.random() returns a double in [0.0, 1.0) — 0 possible, 1 impossible. Multiply by n → [0, n). Cast to int → 0 to n−1. Add low → low to low+n−1.
  • The formula, fully: (int) (Math.random() * (high - low + 1)) + low. Parentheses around the multiplication are essential — the cast must apply to the product, not to Math.random() alone (which would always give 0).
  • Reverse-engineering a range: for (int) (Math.random() * 7) + 3: multiplier 7 → 7 values; added 3 → starts at 3; so 3 through 9.
  • Probability questions: Math.random() < 0.3 is true 30% of the time. (int) (Math.random() * 4) == 0 is true 25% of the time.
  • All Math methods are static: Math.sqrt(x), never x.sqrt() or new Math().

Mistakes that cost points

  • Casting Math.random() before multiplying. (int) Math.random() * 6 is always 0. The cast binds first.
  • Off-by-one in the multiplier. For 1–6 you need 6 values → multiply by 6. For 5–15 you need 11 values → multiply by 11, not 10.
  • Treating Math.pow's result as an int. It's a double. Math.pow(2, 3) + 1 is 9.0.
  • Thinking Math.random() can return 1.0. It can't. The upper bound is exclusive.

Practice questions

Written in the style of the real exam. Try each one before revealing the answer.

Q1 Which expression generates a random integer between 5 and 15, inclusive?
  1. A (int) (Math.random() * 10) + 5
  2. B (int) (Math.random() * 11) + 5
  3. C (int) (Math.random() * 15) + 5
  4. D (int) (Math.random() * 11) + 4
Show answer

Answer: B. 15 - 5 + 1 = 11 possible values, so multiply by 11 and add the low value 5. Option A only reaches 14.

Q2 What is printed by System.out.println(Math.pow(3, 2) + Math.abs(-4));?
  1. A 13
  2. B 13.0
  3. C 9.04
  4. D A compile-time error
Show answer

Answer: B. Math.pow returns a double (9.0). 9.0 + 4 = 13.0.

Q3 What range of values can (int) (Math.random() * 4) * 2 produce?
  1. A 0 to 8
  2. B 0, 2, 4, or 6
  3. C 1 to 8
  4. D 0 to 7
Show answer

Answer: B. (int)(Math.random() * 4) is 0, 1, 2, or 3. Times 2 gives 0, 2, 4, 6.

Key vocabulary

Math.random()
returns a double in [0.0, 1.0)
Math.pow(a, b)
a raised to b, returned as a double
Math.abs(x)
absolute value
Math.sqrt(x)
square root as a double