Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Monte Carlo Experiment

DZone's Guide to

Monte Carlo Experiment

Can you compute pi with random numbers? Read this article, it's worth the gamble!

· Performance Zone
Free Resource

Monte Carlo simulations are a powerful way to solve a bunch of problems using random sampling. These include, and are not limited to, estimating computations that are costly in nature.

Here’s a fun way of computing value of π. Consider a circle of radius r centered at (0,0). This could be bound by a square with corners at (-r, -r), and (r, r). The area of the circle is πr² while the area of the binding square is 4r². If you were to randomly mark n points within the square, the probability p that it would be within the circle will be area of circle / area of square => π/4.

In a Monte Carlo experiment, you would randomly mark n points, and measure the probability p by counting the number of points within the circle. You have,

p/n = πr²/4r²

or

π = 4p/n.

Image title

Go to this page for a live demo of the experiment.

Try it out, you could see that the estimated value of π becomes better with n. Have fun!

Topics:
monte carlo ,pi ,experiment

Published at DZone with permission of Alosh Bennett, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}