Created
August 31, 2021 17:14
-
-
Save corehello/147e89f42cac27abff41e1c280ff1181 to your computer and use it in GitHub Desktop.
use uniform sum distribution to calculate e
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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