我尝试过很多使用蒙特卡罗方法来寻找π的算法。 其中一种解决方案(使用Python)如下:
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
可悲的是,即使使用了十亿的精度,结果也非常不准确(3.141...)。
这种方法的最大精度是多少呢? 我选择蒙特卡罗法的原因是它很容易被分成几个并行部分进行计算。 是否有另一种能够轻松拆分成小块进行计算得出π值的算法呢?