The more raindrops, the more accurate the value of Pi. After each update, the approximate value of Pi is calculated again based on the newly generated raindrops. This can be useful for constructing approximate confidence intervals for the Monte Carlo error. There's also a special button labelled as Let It Rain to continuously generate raindrops as if it were raining. The raindrops are colored blue if they fall inside the quarter circle, otherwise they are colored red. I have added a few buttons to randomly generate 1, 10, 100 or 1000 of raindrops. The app uses Plotly.js to draw the unit square, the quarter circle and the raindrops. I have implemented an interactive simulation in React and Typescript. From here, we can build an equation with a fraction of the quarter circle area over unit square area equal the fraction of the drops inside the quarter circle over the total number of drops. We know that the unit square has an area ofĪnd the quarter circle partly overlaps with this area as defined by the previous formula forĪ q u a r t e r A_ t o t a l n u mb er o f d ro p s g e n er a t e d d ro p s in s i d e t h e q u a r t er c i rc l e Now interestingly, the quarter circle fits perfectly well into a unit square (i.e. the blue area) still has a radius of 1 and its area is defined by the following formula: ![]() We are going to cut the unit circle in four equal segments. The method is based on the mathematical formula for calculating the area of a unit circle (i.e. There are plenty of good articles on the Internet for more detailed information. I'm going to briefly explain the underlying math principle. Let's keep this fraction in mind as we're going to need it in the next step. This is due to the law of large numbers and the fact that we reach ever better distribution. Interestingly, the fraction of raindrops inside the quarter circle over the total number of raindrops will constantly change as we generate more raindrops. This is what it looks with 1000 raindrops: Logically, a lot more raindrops will fall inside the quarter circle than outside of it. blue dots), and some will lie outside of it (i.e. Some raindrops will lie inside the quarter circle (i.e. The raindrops are going to be evenly distributed on the unit square. Now, let's imagine it is raining on this unit square with perfect randomness. Inside this unit square, draw a quarter circle with a radius of 1. Take a sheet of paper and draw a unit square (whose sides have length 1) on it. Let me explain the idea behind the Monte Carlo Simulation with an analogy to rain. In order to refresh some old memories, I decided to implement an interactive simulation in React and TypeScript. ![]() Still, seeing the number getting more and more precise continues to amaze me. Calculating an infinite number like pi doesn't sound like fun for most people. The simple – yet genius – idea behind this concept just blew my mind. When I was in university, I first learned about the Monte Carlo Simulation as way to calculate π \pi π
0 Comments
Leave a Reply. |