蒙特卡罗方法求π值

n = 100000
x0, y0, c0, c1 = n / 2, n / 2, 0, 0
for x in range(n):
    for y in range(n):
        if (x - x0) ** 2 + (y - y0) ** 2 <= x0 * x0:
            c0 += 1
        c1 += 1
print(c0 * 4.0 / c1)

你可能感兴趣的:(DL)