반응형
# 0과 1 사이에서 랜덤하게 뽑은 숫자를 평균적으로 몇 번 더해야 1보다 커질까요?
import numpy as np
import math
import random
def test():
s = 0
cnt = 0
while s < 1:
r = random.random()
s += r
cnt += 1
return cnt
def n_tests(N):
tests = [test() for _ in range(N)]
# print(tests)
m = np.mean(tests)
s = np.std(tests)
print(m, s, N)
def main():
for n in [100, 10000, 1000000, 10000000]:
for _ in range(3):
n_tests(n)
print(math.e)
if __name__ == "__main__":
main()
2.65 0.8170067319184096 100
2.77 0.9884836872705589 100
2.7 0.7681145747868608 100
2.7276 0.8777233277064019 10000
2.7332 0.8765944102034874 10000
2.714 0.8591879887428594 10000
2.718943 0.8757111183209907 1000000
2.716752 0.8754156558435542 1000000
2.717541 0.8746541678394951 1000000
2.7182181 0.8751751029550541 10000000
2.7184342 0.8753957392347529 10000000
2.7182848 0.874877103420223 10000000
2.718281828459045
728x90
'프로그래밍 > Python' 카테고리의 다른 글
파이썬 지수 수치계산방식에 따른 차이 (0) | 2022.04.27 |
---|---|
Q-Pochhammer (0) | 2022.04.08 |
[파이썬초보] AttributeError: 'NoneType' object has no attribute (3) | 2022.01.27 |
find (a, b) such that am + bn = gcd(m, n) (0) | 2022.01.24 |
[PYTHON|SO번역] pip search 실행시에 XMLRPC API is currently disabled due to unmanageable load 에러 발생 (0) | 2021.11.11 |