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 コイン + 今日初回完了時に小ボーナス付与
スケールおよびキャストされた値はゼロであり、その後に4が加算されます。
Math.sqrt(-1)はdoubleの結果を返すのではなく、例外をスローします。
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
特殊なdouble値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.