L9_lecture.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:UWO-PA-Python-Course 作者: dvida 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号