[R] monte carlo method for circle area

Douglas Bates bates at stat.wisc.edu
Mon Mar 31 22:21:03 CEST 2003

Roxana Bravo <neoroxana at yahoo.com.br> writes:

> I ve got an assignment which consists in calculating the area of a
> circle given a certain radius and center using the monte carlo
> method, which means that I have to plot a circle given its
> parameters. Limit the area inside it...with as many sample points as
> possible...and all of this inside a nxn size window, which could be
> 1x1, or any size actually. I wonder how I could just limit the area
> inside the circle..that would be great help for me. Some of you have
> given me several suggestions about plotting a circle using some
> built-in functions but I wonder if its suits my problem since I have
> to somehow limit the circles inner area.

At least you state that this is an assignment :-)  It is close enough
to something that I have been describing to a class that I will answer
here and sent a copy to my class.

One way to do this is to simulate a large number of points uniformly
distributed in the rectangle [-1,1]x[-1,1] and check the distance of
the point from the origin.  The ratio of the number of points of
distance less that 1 from the origin to the total number of points
simulated is an estimate of the ratio of the area of the unit circle
to the area of the rectangle (4, in this case).  We expect the ratio
to be close to pi/4.

Interestingly, in my first simulation I got a ratio that is exactly
pi/4 to 4 significant digits.

> rdat = matrix(runif(10000 * 2, min = -1 , max = 1), nrow = 2)
> sum(colSums(rdat * rdat) < 1) 
[1] 7854
> pi/4 
[1] 0.7853982

More information about the R-help mailing list