def calcPi(n):
""" Calculating Pi using Monte Carlo Integration.
Quickly estimates the first 2 decimals, but it is terribly inefficient for estimating other decimals.
Source: http://www.stealthcopter.com/blog/2009/09/python-calculating-pi-using-random-numbers/
"""
inside = 0.0
for i in range(n):
x = np.random.random()
y = np.random.random()
# Calculate the length of hypotenuse given the sides x and y
if np.hypot(x, y) <= 1:
inside += 1
return 4.0*inside/n
# Run the calcPi function without timing
评论列表
文章目录