The Math Class · A Classe Math
Trace a random range at both ends
- Tickets need integer labels from 4 through 9 inclusive. Use (int)(Math.random() * 6) + 4.
- Scaling gives values from 0 up to but not including 6; truncating gives 0 through 5; shifting gives 4 through 9.
The Math toolbox
- The · A
Mathclass is a library of ready-made math class methods and constants. No import and no object needed — just callMath.something(...). - It covers powers, roots, rounding, absolute value, min/max, and more. Choose the method whose input, output type and mathematical meaning match the task; not every input has an ordinary finite result.
Common methods for AP
Math.abs(x)— absolute value:Math.abs(-5)is5.Math.pow(b, e)—bto the powere:Math.pow(2, 3)is8.0.Math.sqrt(x)— square root:Math.sqrt(9)is3.0.Math.max(a, b)/Math.min(a, b)— the larger / smaller of two values.
Random numbers
Math.random()returns adoublein the range0.0up to (but not including)1.0. Scale and shift it for a range:(int)(Math.random() * 6)gives0–5.- Add
1for a die roll:(int)(Math.random() * 6) + 1gives1–6. For a positive, modest integer width n whose resulting labels fit in int,(int)(Math.random() * n) + startgives n possible integer labels. For 4 through 9 inclusive, n is9 - 4 + 1 = 6.
Building a die roll from Math.random() · Construindo um lançamento de dado de Math.random()
Scale, truncate, then shift to get 1-6.
What does Math.pow(2, 3) equal (as a number)?
2 to the power 3 = 8 (returned as the double 8.0).
Math.sqrt(9) returns a value of which type?
sqrt always returns a double — cast to use it as an int.
Math.random() returns a double in which range?
[0.0, 1.0) — the upper bound 1.0 is never returned.
Which expression gives a random integer from 1 to 6?
Scale by 6 → 0..5, truncate, then +1 → 1..6.
What is Math.abs(-5)?
Absolute value of −5 is 5.
Watch the return types
Math.powand · e Math.sqrtalways return adouble(e.g.8.0, not8). To use the result as anint, you must cast:(int) Math.pow(2, 3).Math.random()is adoubletoo — cast after scaling, not before. Mixing upintand · e doublereturns is a classic Math-class slip.
Math.pow and · e Math.sqrt return a double, not an int. Math.pow(2, 3) is 8.0; store it in a double, or cast to use it as an int. And Math.random() gives a double in [0.0, 1.0) — the upper bound 1.0 is never · nunca returned. Build a whole-number range with (int)(Math.random() * n) + start.
Order the operations that build labels 4 through 9.
Scaling must happen before the cast; otherwise every initial cast is zero.
For labels 4 through 9, substitute u=0 into (int)(u * 6) + 4. What label results?
The scaled and cast value is zero, then 4 is added.
Math.sqrt(-1) throws an exception instead of returning a double result.
It returns the special double value NaN; this is different from an exception.
A random die roll (1–6):
Math.random()→ a double like0.42.Math.random() * 6→0.0up to just under6.0(like2.52).(int)(Math.random() * 6) + 1→ truncates to2, then+1→3.
Carry the reasoning to a new case
- Casting before scaling,
(int)Math.random() * 6 + 4, always gives 4. Check u=0 and a representable value just below 1 in(int)(u * 6) + 4: the possible endpoints are 4 and 9; u=1 is outside the random method's range. - Library methods also have boundary behavior:
Math.sqrt(-1)returns NaN, andMath.abs(Integer.MIN_VALUE)returns that same negative int because its positive magnitude cannot fit in int. The ordinary example abs(-5)=5 still holds.
The · A Math class provides static methods: abs, pow, sqrt, max, min, and random. pow/sqrt return a double (cast for an int). Math.random() returns a double in [0.0, 1.0); build an integer range with (int)(Math.random() * n) + start.