Calculation of Pi Using The Monte Carlo Method
Calculation of Pi Using The Monte Carlo Method
The "Monte Carlo Method" is a method of solving problems Great Pi Day Gifts
using statistics. Given the probability, P, that an event will occur
in certain conditions, a computer can be used to generate those
conditions repeatedly. The number of times the event occurs
divided by the number of times the conditions are generated
should be approximately equal to P.
4*M
pi = ---
N
Comments
Hi eveander This is suresh here, doin my master in computers in ASU .As i'm a
graduate student, actully we had a topic on monte carlo (pi calculation ).when i
had gone through ur site i got stuff regardin this topic i'm interested in.I would like
to know the complete procedure and program code inorder to understand myself
clearly.I would be greatful to u and thanks for consider my above request.
Regards suresh
#!/usr/bin/env python
import random
import math
count_inside = 0
for count in range(0, 10000):
d = math.hypot(random.random(), random.random())
if d < 1: count_inside += 1
count += 1
print 4.0 * count_inside / count
-- Larry Hosken
more digits
Wow, someone sent me some mail about the example. She wrote:
Thank You very much for it! But I would like to please Your help.
Could You tell please, what is nesessery to add to this programm for
to get Pi colculations in such view 3.******** (i mean in your
programm there are 4 numbers after point, but I need 8 numbers).
First, the unhelpful literal answer: To specify that you want to view 8 digits of the
number, you can use a formatted string. That is, instead of something like
print 4.0 * M / N
However, when you try this, you will see it is not so useful: our result is an integer
divided by 10000. Thus, the last few digits are always zero. This leads to the
temptation to use a longer loop to get a more precise answer. But on my wimpy
computer, using a longer loop leads to a too-long run time. And raises questions
about how good my random number generator is. And when should I switch over
to arbitrary-precision numbers? Oh, now my head hurts.
-- Larry Hosken
Magic Numbers
Hi,
I would like to point to the fact that this particular method works especially well
when you make 14 pairs of random numbers. It works very well if you generate
452 numbers.
x=0
y=14
for i = 1, y
do
a=math.random()
b=math.random()
end
print(4*x/y)
I think you can get a generally better approximation using something like this:
S=0
n=100
for i = 1, n
do
x=math.random()
y=(1-x^2)^0.5
S=S+y
end
T=4*S/n
print(T)
-- Szilard Bokros
Practical demonstration
The San Francisco Exploratorium - possibly the coolest kids museum on the
planet - has a large poker-chip-tossing machine on display for kids to calculate Pi.
On my last visit there a crowd of children - not even nerd children (sorry Eve, I'm
stereotyping here...) - were all standing around watching it.
-- Aidan Merritt
Old Method
This method, was known as "The method of the French Lieutenant" or "The
method of generating PI casting stones in a Pond".
MARIO
-- MARIO GIOIA
Add a comment