Over a million developers have joined DZone.

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

Container Monitoring and Management eBook: Read about the new realities of containerization.

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²


π = 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!

Take the Chaos Out of Container Monitoring. View the webcast on-demand!

monte carlo ,pi ,experiment

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}