Monte Carlo estimation
Monte Carlo methods are a broad class of computational algorithms that rely on repeated random sampling to obtain numerical results. One of the basic examples of getting started with the Monte Carlo algorithm is the estimation of Pi.
Estimation of Pi
The idea is to simulate random (x, y) points in a 2-D plane with domain as a square of side 1 unit. Imagine a circle inside the same domain with same diameter and inscribed into the square. We then calculate the ratio of number points that lied inside the circle and total number of generated points. Refer to the image below:
We know that area of the square is 1 unit sq while that of circle is . Now for a very large number of generated points,
The beauty of this algorithm is that we don’t need any graphics or simulation to display the generated points. We simply generate random (x, y) pairs and then check if . If yes, we increment the number of points that appears inside the circle. In randomized and simulation algorithms like Monte Carlo, the more the number of iterations, the more accurate the result is. Thus, the title is “Estimating the value of Pi” and not “Calculating the value of Pi”. Below is the algorithm for the method:
1. Initialize circle_points, square_points and interval to 0.
2. Generate random point x.
3. Generate random point y.
4. Calculate d = x*x + y*y.
5. If d <= 1, increment circle_points.
6. Increment square_points.
7. Increment interval.
8. If increment < NO_OF_ITERATIONS, repeat from 2.
9. Calculate pi = 4*(circle_points/square_points).
The code doesn't wait for any input via stdin as the macro INTERVAL could be changed as per the required number of iterations. Number of iterations are the square of INTERVAL. Also, I've paused the screen for first 10 iterations with getch() and outputs are displayed for every iteration with format given below. You can change or delete them as per requirement.
x y circle_points square_points - pi
INTERVAL = 5 Output : Final Estimation of Pi = 2.56 INTERVAL = 10 Output : Final Estimation of Pi = 3.24 INTERVAL = 100 Output : Final Estimation of Pi = 3.0916
Final Estimation of Pi = 3.16116
This article is contributed by Paras Lehana. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find the maximum possible value of the minimum value of modified array
- Program to calculate the value of sin(x) and cos(x) using Expansion
- Find the value of ln(N!) using Recursion
- Maximum OR value of a pair in an Array without using OR operator
- Floor value Kth root of a number using Recursive Binary Search
- Count of Root to Leaf Paths consisting of at most M consecutive Nodes having value K
- Find length of period in decimal value of 1/n
- Find minimum number of coins that make a given value
- Print all Jumping Numbers smaller than or equal to a given value
- Find the minimum value of m that satisfies ax + by = m and all values after m also satisfy
- Trapezoidal Rule for Approximate Value of Definite Integral
- Maximum value in an array after m range increment operations
- C program to calculate the value of nPr
- Maximum value of an integer for which factorial can be calculated on a machine
- Find minimum value to assign all array elements so that array product becomes greater
- Count numbers with difference between number and its digit sum greater than specific value
- Extract maximum numeric value from a given string | Set 1 (General approach)
- Count of integers of length N and value less than K such that they contain digits only from the given set
- Find minimum value of y for the given x values in Q queries from all the given set of lines
- Maximum value of |arr[i] - arr[j]| + |i - j|
Improved By : MohitBansal3