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
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.
Math.random()를 이용한 주사위 굴리기 구현
스케일하고, 절단한 뒤 이동하여 1-6을 얻음.
Math.pow(2, 3)의 값은 무엇인가(숫자형)?
난이도 2/5 · 140 코인 + 오늘 첫 완료 시 소액 보너스
2의 3제곱 = 8 (double 8.0으로 반환됨).
Math.sqrt(9)는 어떤 타입의 값을 반환하는가?
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
sqrt는 항상 double을 반환하므로 int로 사용하기 위해 캐스트해야 함.
Math.random()는 어느 범위의 double을 반환하는가?
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
[0.0, 1.0) — 상한선인 1.0은 절대 반환되지 않음.
1부터 6까지의 무작위 정수를 제공하는 식은 무엇인가?
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
6배 스케일 → 0..5, 절단 후 +1 → 1..6.
Math.abs(-5)의 값은 무엇인가?
난이도 2/5 · 140 코인 + 오늘 첫 완료 시 소액 보너스
−5의 절댓값은 5이다.
Watch the return types
Math.powandMath.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 upintanddoublereturns is a classic Math-class slip.
Math.pow and 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 returned. Build a whole-number range with (int)(Math.random() * n) + start.
Order the operations that build labels 4 through 9.
난이도 3/5 · 200 코인 + 오늘 첫 완료 시 소액 보너스
Scaling must happen before the cast; otherwise every initial cast is zero.
라벨 4부터 9까지 u=0를 (int)(u * 6) + 4에 대입하십시오. 어떤 라벨이 결과로 나오나요?
난이도 2/5 · 140 코인 + 오늘 첫 완료 시 소액 보너스
확장되고 변환된 값은 0이며, 여기에 4이 추가됩니다.
Math.sqrt(-1)는 이중 정밀도 결과를 반환하지 않고 예외를 발생시킵니다.
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
특수 부동소수점 값 NaN을 반환하며, 이는 예외와 다릅니다.
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 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.