Monte Carlo Method and pi

Nick Smallbone nick at nick8325.freeserve.co.uk
Thu Jul 8 17:53:12 EDT 2004


"Karl Pech" <KarlPech at users.sf.net> wrote in message
news:ccke8p$c0h$06$1 at news.t-online.com...
> 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

This line is wrong. I think it should be sy += math.sqrt(1 - x ** 2). Doing
that gives me ~3.1 for 1000 iterations.

Nick





More information about the Python-list mailing list