Skip to content

Instantly share code, notes, and snippets.

@corehello
Created August 31, 2021 17:14
Show Gist options
  • Save corehello/147e89f42cac27abff41e1c280ff1181 to your computer and use it in GitHub Desktop.
Save corehello/147e89f42cac27abff41e1c280ff1181 to your computer and use it in GitHub Desktop.
use uniform sum distribution to calculate e
def uniform_sum_distribution_for_e(N,target):
count_sum = 0
import random
for i in range(N):
sum = 0
while True:
j = random.random()
sum+=j
count_sum+=1
if sum > target:
sum = 0
break
return count_sum/N
# when target = 1, it should be about e = 2.71828
# when target = 2, it should be about e^2 -e ~= 4.67077
# https://mathworld.wolfram.com/UniformSumDistribution.html
# this method do not have much precision
uniform_sum_distribution_for_e(100000,2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment