使用蒙特卡洛查找PI数字 [英] Finding PI digits using Monte Carlo

查看:138
本文介绍了使用蒙特卡洛查找PI数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了许多使用蒙特卡洛(Monte Carlo)查找π的算法. 解决方案之一(在Python中)是这样的:

I have tried many algorithms for finding π using Monte Carlo. One of the solutions (in Python) is this:

def calc_PI():
    n_points = 1000000
    hits = 0

    for i in range(1, n_points):
        x, y = uniform(0.0, 1.0), uniform(0.0, 1.0)

        if (x**2 + y**2) <= 1.0:
            hits += 1

    print "Calc2: PI result", 4.0 * float(hits) / n_points

可悲的是,即使使用10亿,精度也非常差( 3.141 ... ).

The sad part is that even with 1000000000 the precision is VERY bad (3.141...).

这是此方法可以提供的最大精度吗? 我选择Monte Carlo的原因是,很容易将其拆分为平行部分. π有另一种容易分解并计算的算法吗?

Is this the maximum precision this method can offer? The reason I choose Monte Carlo was that it's very easy to break it in parallel parts. Is there another algorithm for π that is easy to break into pieces and calculate?

推荐答案

这是Monte Carlo的经典示例.但是,如果您尝试将pi的计算分解为多个平行部分,为什么不只是使用一个无限级数并让每个核取一个范围,然后将结果累加呢?

This is a classic example of Monte Carlo. But if you're trying to break the calculation of pi into parallel parts, why not just use an infinite series and let each core take a range, then sum the results as you go?

http://mathworld.wolfram.com/PiFormulas.html

这篇关于使用蒙特卡洛查找PI数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆