Monte Carlo Method and pi

Karl Pech KarlPech at users.sf.net
Thu Jul 8 23:30:01 CEST 2004


Hi,


I got a task there I have to compute pi using the
Method above.

So I wrote the following program:
---
import random
import math

n = long(raw_input("Please enter the number of iterations: "))
sy = 0                                   # sum of the function-values

for i in range(0, n):
  x = random.random()                    # coordinates
  sy += math.sqrt(1-math.sqrt(x))        # calculate y and add to sy

print 4*sy/n                             # compute pi
---

Unfortunately, even for n = 2000000 the result is ~2,13... .
It converges very slow to pi and I don't know why.

Please help me!

Regards
Karl





More information about the Python-list mailing list