## Tutorial

• 04/11/2022
• Public Content
Contents

# Calculating the Value of Pi using a Monte Carlo Method

We will be using a program that calculates the value of the mathematical constant π (Pi) using a Monte Carlo method, so named because it uses random numbers. Imagine a square piece of paper two units across (the actual unit doesn’t matter). On this paper draw a circle whose diameter is two units (radius one unit). The area of the circle is πr
2
, but since the radius (r) is 1, and we know the square’s area is 4 (2x2), the ratio of the circle’s area to that of the square’s area is π/4. But how do we get the area of the circle? What we can do is pick random points on the paper and count the points that are within the circle. To make this easier, we’ll use the center of the square as the origin and generate random values for X and Y coordinates between zero and 1, so we’re looking at a quarter of the circle/square. If X
2
+Y
2
(distance between the point and the origin) is less than or equal to 1, the point is within the circle. Count the number of random points that are within the circle and divide that by the total number of points; the result is π/4. For a more detailed explanation of this technique, see http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/compute-pi.html

#### Product and Performance Information

1

Performance varies by use, configuration and other factors. Learn more at www.Intel.com/PerformanceIndex.