Suppose I sample values uniformly at random from the unit interval. How many samples should I expect to take before the sum of my sampled values exceeds unity?1

Let N be the (random) number of samples taken when the sum first exceeds unity. Then N has expected value E[N] equal to Euler’s number e2.718282. This can be verified approximately via simulation:

simulate <- function(run) {
  tot <- 0
  N <- 0
  while (tot < 1) {
    tot <- tot + runif(1)
    N <- N + 1
  }
  N
}

set.seed(0)
mean(sapply(1:1e5, simulate))
## [1] 2.7183

To see why E[N]=e, let (Xi)i=1 be an infinite sequence of random variables with uniform distributions over the unit interval. Then the probability that N exceeds any non-negative integer n is Pr(N>n)=Pr(X1+X2++Xn<1). Consider the unit (hyper)cube in Rn. Its vertices comprise the origin, the standard basis vectors e1,e2,,en, and the sums of two or more of these basis vectors. The convex hull of {0,e1,e2,,en} forms an n-simplex with volume 1/n!. The interior of this simplex is precisely the set {X1,X2,,Xn[0,1]:X1+X2++Xn<1}. It follows that Pr(X1+X2++Xn<1)=1/n! and therefore Pr(N>n)=1/n! from above. Now Pr(N=n)=Pr(N>n1)Pr(N>n) for each n1. Thus, since Pr(N>0)=1 (and, by convention, 0!=1), we have E[N]=n=1nPr(N=n)=n=1n(Pr(N>n1)Pr(N>n))=Pr(N>0)+n=1Pr(N>n)=1+n=11n!=n=01n!=e. The final equality comes from evaluating the Maclaurin series expansion of ex at x=1.


  1. Grant Sanderson mentions this problem in this Numberphile video. ↩︎