Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Monte Carlo approximation of PI

by robartes (Priest)
on Apr 04, 2003 at 11:40 UTC ( [id://248032]=note: print w/replies, xml ) Need Help??


in reply to Monte Carlo approximation of PI

Very nice, but shouldn't it be rand()**2 + rand()**2 <= 1? Given the fact that the range of numbers returned from a random generator is discrete, given enough iterations, doing just  < 1 would underestimate pi ever so slightly. Then again, nobody would probably run this algorithm long enough to see an effect from this :).

CU
Robartes-

Replies are listed 'Best First'.
Re: Re: Monte Carlo approximation of PI
by Anonymous Monk on Apr 04, 2003 at 12:54 UTC
    It is probable that imperfections in Perl's rand will result in a consistent slight theoretical bias. It would also take an insane amount of time to have any chance of detecting this fact. Remember that to get the error down to 1 part in n, the number of iterations that you need is proportional to n*n. (The appropriate constant depends on what probability of being outside of your interval you are willing to accept.) A theoretical error on the order of one part in 50 million takes 2,500 trillion steps to find. Assuming that you can invoke the necessary code 100 million times per section (probably rather optimistic on today's hardware), it would take on the order of a year or so to find it.

    If the error is smaller than that, it would take longer...

    (I don't have sufficient interest to find out what Perl's rand algorithm is to calculate the order of magnitude of the exact theoretical bias. It will depend on how close together the grid of possible answers are that rand can return.)

Re: Monte Carlo approximation of PI
by Abigail-II (Bishop) on Apr 04, 2003 at 12:03 UTC
    But if you want to factor in the discreteness, with the same argumenation it follows that using <= 1 will lead to an overestimated pi.

    Abigail

      Indeed, well spotted. So to make the algorithm even slower, we might actually have to run it twice, once with  < 1 and once with  <= 1, and then take the average of the two. Oh well :).

      CU
      Robartes-

        I just ran a simulation with 16 million uniformely distributed points, and using both < 1 and <= 1 give estimates of 3.141020.

        Round-off errors probably pay their toll as well, even on my 64bit perl.

        Abigail

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://248032]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-24 19:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found