import math
import random as rnd
from itertools import accumulate

# K. G. Russell. Estimating the value of e by simulation. The American Statistician, 45(1):66, February 1991.

S, N, eSum, eSqSum = 20, 100000, 0, 0
for i in range(N): # simulation loop
    # generate S uniform random numbers U_k
    A = list(accumulate([rnd.random() for s in range(S)])) 
    
    # find min(n|U_1+U_2+...+U_n>1)
    e = [idx for idx, k in enumerate(A) if k > 1][1]       

    eSum += e          # accumulate e
    eSqSum += pow(e,2) # accumulate e^2
    
print("E[min(n|U_1+...+U_n>1)] \t= "+
      '%.4f' % float(eSum/N) +
      "\t(e = "+str(math.e)+")")
print("Std[min(n|U_1+...+U_n>1)] \t= "+
      '%.4f' % math.sqrt(1/N*eSqSum-pow(1/N*eSum,2))+
      "\t(= [e(3-e)]^0.5)")