Using the Math Class

Math's Static Methods

Math is a built-in class of static methods — no object, no new, just call it directly by its class name:

Math.abs(-4);     // 4 — absolute value
Math.pow(2, 3);   // 8.0 — 2 to the power of 3
Math.sqrt(9);     // 3.0
Math.random();    // a double >= 0.0 and < 1.0

These all return a value, so — same rule as the last lesson — you should actually use what they return. (Math lives in java.lang, so it never needs an import — Lesson 3.3.)

More Math Methods Robot Code Uses

MethodWhat it gives youExample
Math.max(a, b) / Math.min(a, b)the larger / smaller of two valuesMath.max(3, 7) → 7
Math.round(x)x rounded to the nearest whole numberMath.round(2.6) → 3
Math.floor(x) / Math.ceil(x)x rounded down / up, as a doubleMath.floor(2.6) → 2.0
Math.signum(x)the sign of x: -1.0, 0.0, or 1.0Math.signum(-0.4) → -1.0
Math.PIthe constant π — a value, not a method, so no parentheses3.141592653589793
Math.toRadians(degrees)an angle in degrees converted to radiansMath.toRadians(180) → 3.141592653589793
Math.hypot(x, y)√(x² + y²) — the straight-line distanceMath.hypot(3, 4) → 5.0
Math.atan2(y, x)the angle, in radians, pointing at (x, y) — note y comes firstMath.atan2(1, 1) → 0.7853981633974483
Math.clamp(value, min, max)value, kept inside the range min to maxMath.clamp(1.4, -1.0, 1.0) → 1.0

Math.clamp is the one you'll use most for motors — it keeps a computed throttle from ever leaving the legal -1.0 to 1.0 range:

// never lower than -1.0 or higher than 1.0
double throttle = Math.clamp(joystickValue * 1.5, -1.0, 1.0);

Java 17 note: Math.clamp was added in Java 21. On Java 17, write Math.max(-1.0, Math.min(1.0, value)) instead — it gives the same result.

Random Numbers in a Range

Math.random() alone gives a double in [0.0, 1.0) — including 0.0, never reaching 1.0. That's rarely useful on its own; you stretch and shift it to get the range you actually want:

int rnd = (int)(Math.random() * 10);      // an int from 0-9 (10 possible values)
int rnd2 = (int)(Math.random() * 10) + 1; // an int from 1-10 (shifted up by 1)

The general recipe: multiply by how many values you want, cast to int to drop the decimal, then add the minimum to shift the whole range up.

// A random autonomous delay between 1 and 4 seconds
double delay = Math.random() * 3.0 + 1.0; // [1.0, 4.0)

// A random starting position index, 0 through 2 (3 choices)
int startPos = (int)(Math.random() * 3);

The parentheses matter: (int)(Math.random() * 10) casts the whole scaled expression; (int)Math.random() * 10 casts Math.random() alone first — which truncates it to 0 every time, then multiplies by 10, always giving 0.

Common Pitfalls

  • Forgetting the parentheses around the scaled expression. (int)Math.random() * 10 always evaluates to 0 — the cast has to wrap the whole Math.random() * range expression.
  • Off-by-one on the range size. For an inclusive range like 1-10, the range size is 10, not 9 — count how many distinct values you actually want.
  • Ignoring the return value. Math.abs(x); on its own line compiles but does nothing useful — the result has to be stored or used.

Key Takeaways

  • Math methods are static — call them as Math.methodName(...), no object needed.
  • Math.random() returns a double in [0.0, 1.0) — 0.0 possible, 1.0 never reached.
  • To get a random int in a range: (int)(Math.random() * range) + min, where range is the count of possible values.
  • The parentheses around Math.random() * range are required — casting only Math.random() breaks the formula.
  • Math also gives you max/min, round, floor/ceil, signum, PI, toRadians, hypot, atan2, and — on Java 21+ — clamp.

Derived from csawesome-2026: unit-1-using-objects-and-methods/1.11-using-the-math-class.md (CSA 1.11)