select numbers between 0 and 1 randomly until sum is > 1
The expected # of selections needed is equal to e
# setup importsimport mathfrom typing import Tupleimport numpy as npimport pandas as pdfrom tqdm.notebook import trange
def run_experiment() -> Tuple[float, int]: s =0 n =0while s <=1: n +=1 s += np.random.uniform(0, 1)return s, nlogs = [run_experiment() for _ in trange(2_000_000)]df = pd.DataFrame(logs, columns=["sum", "nums_to_one"])df.head()
sum
nums_to_one
0
1.160454
2
1
1.099141
2
2
1.171709
3
3
1.664728
3
4
1.291741
2
euler's number: 2.718282
avg num to one: 2.717664
avg sum*2: 2.718284