Upper-Tail Probability of 1,000 Die Rolls
Hard · Statistics & Math · Machine Learning Engineer · Onsite
Problem Statement
Suppose a fair six-sided die (faces 1 through 6, each occurring with probability 1/6) is tossed 1,000 times independently, and let S denote the sum of all 1,000 outcomes.
Implement probability_sum_exceeds(t: int) -> float to return the estimated probability P(S > t) that the total S is strictly greater than the integer cutoff t:
- Exact support checks: Because every roll lands in
{1, 2, 3, 4, 5, 6}, the totalSis always an integer in[1000, 6000].- Whenever
t < 1000,S > tholds for every possible sequence of rolls, so return1.0. - Whenever
t >= 6000,S > tcannot occur for any sequence of rolls, so return0.0.
- Whenever
- Continuity-corrected Gaussian approximation: For
1000 <= t < 6000, modelSvia the Central Limit Theorem using the exact single-roll meanE[X] = 7/2 = 3.5and single-roll varianceVar(X) = 35/12scaled across1,000independent rolls (E[S] = 3500,Var(S) = 1000 * 35 / 12).- Because
Stakes integer values only,S > tis identical toS >= t + 1. Applying the half-unit continuity correction places the continuous boundary att + 0.5, giving standardized scorez = (t + 0.5 - 3500) / sqrt(1000 * 35 / 12). - Return the standard normal upper-tail area
1 - Phi(z), evaluated in a numerically stable form (such as0.5 * math.erfc(z / sqrt(2))) so large positivezvalues do not round prematurely to0.0due to floating-point subtraction.
- Because
Examples
Example 1
Input: t = 900
Output: 1.0
Even if all 1,000 tosses show 1, their sum is 1,000, which strictly exceeds 900. Therefore P(S > 900) = 1.0.
Example 2
Input: t = 3581
Output: 0.065638306
S > 3581 corresponds to S >= 3582, so the continuity-corrected cutoff is 3581.5. With mean 3500 and standard deviation sqrt(1000 * 35 / 12) ≈ 54.006172, we obtain z = 81.5 / 54.006172 ≈ 1.509087 and 1 - Phi(z) ≈ 0.0656383.
Example 3
Input: t = 6200
Output: 0.0
Even rolling 6 on every single toss yields S = 6,000, so exceeding 6,200 is impossible and P(S > 6200) = 0.0.
Constraints
-10^9 <= t <= 10^9(tis an integer).- Return a
floatin[0.0, 1.0]matching1.0fort < 1000,0.0fort >= 6000, and the continuity-corrected normal upper tail within10^-6relative/absolute tolerance for1000 <= t < 6000.
Step-by-step walkthrough
Clarify the rules and target probability
We toss a fair six-sided die
1,000independent times and add up all1,000outcomes to formS. Given an integer cutofft,probability_sum_exceeds(t)estimatesP(S > t).Three foundational details govern the math:
- Strict inequality (
S > t): BecauseSonly takes whole-number values,S > tis identical toS >= t + 1rather thanS >= t. - Gaussian approximation: Enumerating all
6^1000equally likely outcome sequences (a779-digit integer) is out of reach, so we approximateSinside its interior range using the standard normal cumulative distribution functionPhi. - Support check before standardizing: Every toss lands in
{1, 2, 3, 4, 5, 6}, which locksSinside[1000, 6000]. Cutoffs below1,000or at/above6,000have exact answers1.0and0.0.
- Strict inequality (
Moments of a single fair die roll
Let
Xbe the face shown on one toss, taking each value in{1, 2, 3, 4, 5, 6}with probability1/6.- Expected value (
E[X]): By symmetry around3.5(or direct averaging),E[X] = (1 + 2 + 3 + 4 + 5 + 6) / 6 = 21 / 6 = 7 / 2 = 3.5. - Second moment (
E[X^2]): Average the six squared faces:E[X^2] = (1^2 + 2^2 + 3^2 + 4^2 + 5^2 + 6^2) / 6 = (1 + 4 + 9 + 16 + 25 + 36) / 6 = 91 / 6. - Variance (
Var(X)): ApplyVar(X) = E[X^2] - (E[X])^2over common denominator12:Var(X) = 91/6 - (7/2)^2 = 182/12 - 147/12 = 35/12 ≈ 2.917. - Standard deviation (
SD(X)): Take the square root so spread is measured in die points rather than squared points:SD(X) = sqrt(35 / 12) ≈ 1.708.
Store
35 / 12directly in Python rather than a rounded decimal so multiplying by1,000stays exact.NUM_ROLLS = 1000 # E[X] = (1 + 2 + 3 + 4 + 5 + 6) / 6 = 21 / 6 = 3.5 SINGLE_ROLL_MEAN = 3.5 # Var(X) = E[X^2] - E[X]^2 = 91/6 - 49/4 = 35/12 (keep exact fraction to avoid rounding error) SINGLE_ROLL_VARIANCE = 35 / 12- Expected value (
Scale mean and variance across 1,000 independent rolls
Write
S = X_1 + X_2 + ... + X_1000for the sum of all1,000tosses:- Expected sum (
E[S]): By linearity of expectation, expectations add regardless of dependence:E[S] = 1000 * 3.5 = 3500. - Sum variance (
Var(S)): Because the tosses are independent, all cross-covariances vanish and variances add:Var(S) = 1000 * (35 / 12) = 8750 / 3 ≈ 2916.67. - Sum standard deviation (
SD(S)): Take the square root ofVar(S):SD(S) = sqrt(1000 * 35 / 12) ≈ 54.006.
Notice the scaling difference: the mean grows linearly by
1,000, whereas the standard deviation grows only bysqrt(1000) ≈ 31.62because high and low rolls cancel each other out.Because
Ssums1,000independent, identically distributed bounded variables with positive variance, the Central Limit Theorem (CLT) approximatesSclosely byNormal(mean = 3500, variance = 8750 / 3).- Expected sum (
Apply continuity correction on the normal curve
Sis discrete (integers only), while the normal density is smooth. Picture the exact probability mass function as a histogram in which each integerkoccupies a unit-width bar[k - 0.5, k + 0.5]:Integer outcomes: ... | t - 1 | t | t + 1 | t + 2 | ... Continuous intervals: ... [t-1.5, t-0.5] [t-0.5, t+0.5] [t+0.5, t+1.5] [t+1.5, t+2.5] ... ^ ^ excluded | |---> S > t means S >= t + 1, (S == t) so integrate from t + 0.5 rightward ->Because
S > tomits the bar centered attand begins with the bar centered att + 1, the continuous split point sits att + 0.5:z_score = (t + 0.5 - 3500) / sqrt(1000 * 35 / 12) P(S > t) ≈ 1 - Phi(z_score)Evaluating
1.0 - Phi(z)directly in 64-bit float loses precision oncez > 8.3, becausePhi(z)rounds to1.0and1.0 - 1.0collapses to0.0. Calling0.5 * math.erfc(z / sqrt(2))evaluates the upper tail directly without subtraction.import math def normal_upper_tail(z_score: float) -> float: """Return P(Z > z_score) = 1 - Phi(z_score) for a standard normal variable Z.""" # Using erfc directly avoids 1.0 - Phi(z_score), which suffers catastrophic # cancellation and rounds to 0.0 as soon as Phi(z_score) rounds to 1.0 (around z > 8.3). return 0.5 * math.erfc(z_score / math.sqrt(2.0))Check physical sum bounds before standardizing
Before computing any z-score, compare
tagainst[1000, 6000]:- When
t < 1000: Even a thousand consecutive1s giveS = 1000 > t, soP(S > t) = 1.0. Use strict<because att = 1000, rolling all ones yieldsS = 1000, which does not strictly exceed1000. - When
t >= 6000: Even a thousand consecutive6s giveS = 6000, which never strictly exceeds6000, soP(S > t) = 0.0. Use>=here.
That settles Example 1 (
t = 900 -> 1.0) and Example 3 (t = 6200 -> 0.0) immediately without touching a normal table.def probability_sum_exceeds(t: int) -> float: """Estimate P(S > t) for the sum S of 1,000 fair 6-sided die rolls.""" # Phase 1: Check physical bounds of S, which can only take integers in [1000, 6000]. min_sum = NUM_ROLLS * 1 max_sum = NUM_ROLLS * 6 # Strictly '<': at t = 1000, all-ones gives S = 1000, so S > 1000 is NOT 100% certain. if t < min_sum: return 1.0 # '>=': S can never exceed 6000, so even at t = 6000 the event S > 6000 is impossible. if t >= max_sum: return 0.0 # Phase 2: Scale mean and variance from 1 roll to 1,000 independent rolls. sum_mean = NUM_ROLLS * SINGLE_ROLL_MEAN # 3500.0 sum_variance = NUM_ROLLS * SINGLE_ROLL_VARIANCE # 8750 / 3 ~ 2916.67 (variances add by independence) sum_std_dev = math.sqrt(sum_variance) # ~ 54.01 (grows with sqrt(NUM_ROLLS)) # Phase 3: Continuity correction + standardization. # Because S is an integer, S > t is the exact same event as S >= t + 1. # On a continuous curve where integer k spans [k - 0.5, k + 0.5], the bar for t + 1 starts at t + 0.5. cutoff = t + 0.5 z_score = (cutoff - sum_mean) / sum_std_dev return normal_upper_tail(z_score)- When
Trace the sample thresholds
Let's walk through Examples 1, 2, and 3 plus the exact center
t = 3500:1) t = 900 (Example 1): 900 < min_sum (1000) -> returns 1.0 immediately. 2) t = 3500 (right at the expected sum): cutoff = 3500.5 z_score = (3500.5 - 3500.0) / 54.006172 = +0.009258 answer = 1 - Phi(0.009258) ≈ 0.496307 (Slightly under 0.5 because S == 3500 has probability ~0.0074 and does not satisfy S > 3500!) 3) t = 3581 (Example 2, ~1.51 SD above the mean): cutoff = 3581.5 z_score = (3581.5 - 3500.0) / 54.006172 = +1.509087 answer = 1 - Phi(1.509087) ≈ 0.065638 4) t = 6200 (Example 3): 6200 >= max_sum (6000) -> returns 0.0 immediately.Symmetry provides a quick sanity check on
+ 0.5: mapping each tossX_i -> 7 - X_iturnsSinto7000 - S, soP(S > t) + P(S > 6999 - t)must equal1.0. With+ 0.5, the two cutoffst + 0.5and7000 - t - 0.5sit at equal and opposite offsets from3500, making the two normal tails add up to1.0!Complete solution
from typing import Any, Dict, List, Optional, Set, Tuple import math # Constants for 1,000 independent rolls of a fair 6-sided die {1, 2, 3, 4, 5, 6}. NUM_ROLLS = 1000 # E[X] = (1 + 2 + 3 + 4 + 5 + 6) / 6 = 21 / 6 = 3.5 SINGLE_ROLL_MEAN = 3.5 # Var(X) = E[X^2] - E[X]^2 = 91/6 - 49/4 = 35/12 (keep exact fraction to avoid rounding error) SINGLE_ROLL_VARIANCE = 35 / 12 def normal_upper_tail(z_score: float) -> float: """Return P(Z > z_score) = 1 - Phi(z_score) for a standard normal variable Z.""" # Using erfc directly avoids 1.0 - Phi(z_score), which suffers catastrophic # cancellation and rounds to 0.0 as soon as Phi(z_score) rounds to 1.0 (around z > 8.3). return 0.5 * math.erfc(z_score / math.sqrt(2.0)) def probability_sum_exceeds(t: int) -> float: """Estimate P(S > t) for the sum S of 1,000 fair 6-sided die rolls.""" # Phase 1: Check physical bounds of S, which can only take integers in [1000, 6000]. min_sum = NUM_ROLLS * 1 max_sum = NUM_ROLLS * 6 # Strictly '<': at t = 1000, all-ones gives S = 1000, so S > 1000 is NOT 100% certain. if t < min_sum: return 1.0 # '>=': S can never exceed 6000, so even at t = 6000 the event S > 6000 is impossible. if t >= max_sum: return 0.0 # Phase 2: Scale mean and variance from 1 roll to 1,000 independent rolls. sum_mean = NUM_ROLLS * SINGLE_ROLL_MEAN # 3500.0 sum_variance = NUM_ROLLS * SINGLE_ROLL_VARIANCE # 8750 / 3 ~ 2916.67 (variances add by independence) sum_std_dev = math.sqrt(sum_variance) # ~ 54.01 (grows with sqrt(NUM_ROLLS)) # Phase 3: Continuity correction + standardization. # Because S is an integer, S > t is the exact same event as S >= t + 1. # On a continuous curve where integer k spans [k - 0.5, k + 0.5], the bar for t + 1 starts at t + 0.5. cutoff = t + 0.5 z_score = (cutoff - sum_mean) / sum_std_dev return normal_upper_tail(z_score)✅ Submit now — all tests should pass.
Intuition & how it works
Imagine tossing one die: the outcome jumps anywhere from 1 to 6. When you toss 1,000 dice and add them up, lucky 6s and unlucky 1s cancel out so consistently that the total clusters in a tight bell-shaped curve centered at 3,500. This is the Central Limit Theorem (CLT), which says that the sum of many independent, identically distributed random numbers with finite spread looks closely like a Gaussian (normal) bell curve. Before touching the bell curve, though, we enforce physical bounds: 1,000 dice must total at least 1,000 (all ones) and at most 6,000 (all sixes).
Computing the exact answer by listing every roll sequence is hopeless because 6^1000 has 779 digits. Yet plugging t blindly into a continuous bell-curve formula creates two bugs: it overlooks impossible/certain cutoffs outside [1000, 6000] (such as t = 900 or t = 6200), and it misplaces integer boundaries unless we apply continuity correction (shifting an integer cutoff by 0.5 so each whole number k owns the unit-width slice [k - 0.5, k + 0.5] beneath the continuous curve).
Let's see how this plays out using one toss's expected value (long-run average) 3.5 and variance (average squared deviation from the mean) 35/12, which scale across 1,000 independent tosses to mean = 3500, variance = 1000 * 35/12 ≈ 2916.67, and standard deviation (the square root of variance, measuring typical spread in sum points) sqrt(2916.67) ≈ 54.01:
t = 900:900lies below the minimum possible total1,000, soS > 900holds with probability1.0immediately.t = 3500:S > 3500on integers meansS >= 3501, whose histogram slice begins at3500.5. Converting3500.5into a z-score (how many standard deviations from the mean a cutoff sits) givesz = (3500.5 - 3500) / 54.01 ≈ +0.0093, leaving0.4963in the upper tail (slightly under0.5becauseS == 3500does not count).t = 3581: the slice for3582begins at3581.5, givingz = (3581.5 - 3500) / 54.01 ≈ +1.5091and upper-tail probability0.0656(6.56%).t = 6200:6,200exceeds the maximum possible total6,000, returning0.0immediately.
Complexity
- Time:
O(1). We execute two boundary comparisons, a handful of arithmetic operations, and one call tomath.erfc(the complementary error function, a constant-time library routine that computes standard normal upper-tail areas directly); runtime never loops over the1,000tosses or depends ont. - Space:
O(1). Only a fixed set of scalar floats (min_sum,max_sum,sum_mean,sum_variance,sum_std_dev,cutoff,z_score) is allocated.
Edge cases
- Below the minimum possible total (
t < 1000, includingt = 900and negative cutoffs): every outcome totals at least1,000, returning1.0. Att = 1000itself, rolling a thousand1s yieldsS = 1000, soS > 1000is not certain (<rather than<=). - At or above the maximum possible total (
t >= 6000, includingt = 6200): no sequence can strictly exceed6,000, returning0.0(>=rather than>). Without this guard, a continuous Gaussian curve assigns tiny positive density past the true maximum. - Right at the expected sum (
t = 3500): returns~0.4963rather than0.5, sinceS == 3500(probability~0.0074) is excluded by strict>. - Deep upper tail (
tbetween4000and5999): evaluating1.0 - Phi(z)through the cumulative distribution function (CDF)Phi(z)(the probability that a standard normal variable is at mostz) suffers catastrophic cancellation (loss of precision when subtracting two nearly equal floats) and collapses to0.0oncez > 8.3(t >= 3948). Calling0.5 * math.erfc(z / sqrt(2))preserves positive 64-bit floats all the way tot = 5576(z ≈ 38.4). - Reflection symmetry: pairing each toss
X_iwith7 - X_iprovesP(S > t) + P(S > 6999 - t) = 1for every integert; the+ 0.5continuity correction preserves this identity to machine precision.
Common mistakes
- Confusing variance with standard deviation. One toss has variance
35/12 ≈ 2.917and standard deviationsqrt(35/12) ≈ 1.708; plugging2.917in as the standard deviation inflates the spread by71%. - Multiplying standard deviation linearly by
1,000. Independent variances add (Var(S) = 1000 * Var(X)), so standard deviation scales bysqrt(1000) ≈ 31.62to54.01, not1000 * 1.708 = 1708. - Omitting
+ 0.5or shifting tot - 0.5. Usingt(no correction) ort - 0.5(which estimatesP(S >= t)instead ofP(S > t)) introduces an error of~0.0037to~0.0074near the center — more than100xlarger than the corrected approximation's peak error of~0.000026. - Skipping support checks. Plugging
t = 900ort = 6200straight into a z-score misses the physical bounds[1000, 6000]. - Computing
1.0 - cdf(z)at largez. Roundscdf(z)to1.0pastz ≈ 8.3and returns0.0prematurely.
Follow-up questions
- What happens to the probability of exceeding 900 if only 150 rolls are performed instead of 1,000? With
150tosses, the achievable totals span[150, 900], with expected sum150 * 3.5 = 525and standard deviationsqrt(150 * 35 / 12) ≈ 20.92. Now900sits right at the maximum achievable total (150 * 6 = 900) instead of below the minimum! Because no sequence of150tosses can strictly exceed900, the probability flips from1.0all the way to0.0— whereas an unguarded Gaussian formula would wrongly report1 - Phi((900.5 - 525) / 20.92) ≈ 2e-72instead of exact zero (and if>= 900were asked, only150straight sixes would qualify, with exact probability6^-150 ≈ 4.9e-117). - Why does the Gaussian estimate lose relative accuracy in the extreme upper tail, and which analytical bounds or approximations work better there? The Central Limit Theorem guarantees small absolute error (
< 3e-5everywhere), so relative error blows up once the true tail probability shrinks far below1e-5: the normal tail is~0.9%high att = 3700(z ≈ 3.7),~48%high att = 4000(z ≈ 9.3), more than800xhigh att = 4500, and positive even past6,000. In deep tails, use a Chernoff bound (an exponential upper bound optimized over the moment-generating functionE[e^(theta * X)]that respects bounded dice support), a saddlepoint approximation (an exponentially tilted normal refinement whose relative error stays tiny all the way to the boundary), or exact combinatorial counting. - How can you approximate the tail probability mentally—with no calculator or lookup table—when the cutoff sits roughly two standard deviations above the expected sum? Two standard deviations above
3,500lands near3500 + 2 * 54 = 3608. By the 68-95-99.7 empirical rule (which recalls that a normal bell curve contains roughly68%,95%, and99.7%of its mass within1,2, and3standard deviations of the mean), about5%of total probability lies outside±2standard deviations, and symmetry puts half of that in the upper tail:~2.5%. Using the slightly sharper95.45%figure for±2standard deviations gives(100% - 95.45%) / 2 ≈ 2.28%, right beside the continuity-corrected value0.0223(2.23%). - What algorithm computes the exact rational or floating-point value of P(S > t) across 1,000 rolls without relying on a normal curve? Two clean exact approaches work well: (1) dynamic programming (iteratively tabulating the exact integer count of roll sequences for every achievable sum from
1toss up to1,000tosses using a 6-term sliding-window sum in roughly6 * 1000^2 ≈ 6 * 10^6integer additions), or (2) inclusion-exclusion (countingn-tuples of positive integers summing to at mostmviaC(m, n)and alternatingly subtracting/adding tuples wherekdice exceed6). PairingX_i <-> 7 - X_irewritesP(S > t) = P(S <= 7n - 1 - t), requiring at most(6000 - 1000) // 6 ≈ 834binomial terms in Python's arbitrary-precision arithmetic:
```python import math
def exact_probability_exceeds(t: int, n: int = 1000) -> float: # Reflection symmetry X_i <-> 7 - X_i gives P(S > t) = P(S <= 7n - 1 - t). m = 7 * n - 1 - t if m < n: return 0.0 if m >= 6 * n: return 1.0 valid_sequences = 0 for k in range((m - n) // 6 + 1): ways = math.comb(n, k) * math.comb(m - 6 * k, n) valid_sequences += ways if k % 2 == 0 else -ways return valid_sequences / (6 ** n) ```
Follow-up questions
- What happens to the probability of exceeding 900 if only 150 rolls are performed instead of 1,000?
- Why does the Gaussian estimate lose relative accuracy in the extreme upper tail, and which analytical bounds or approximations work better there?
- How can you approximate the tail probability mentally—with no calculator or lookup table—when the cutoff sits roughly two standard deviations above the expected sum?
- What algorithm computes the exact rational or floating-point value of P(S > t) across 1,000 rolls without relying on a normal curve?
Similar questions
- Eddington Number of Daily Rides
- Build a Terminal-Ready Tetris Engine
- Shortest Connection Degree in a Social Network
- Shortest Substring Run-Length Encoding and Decoding
- Validate and Expand an IPv6 Address
- Handling an Inconclusive A/B Test Readout