Pi () is a mathematical constant used in different applications in mathematics and physics. One of its common applications is that it represents the ratio of the circumference of a circle to its diameter as shown in the following equation:
Here is the circumference and is the radius of the circle. It is approximated as . is an irrational number. It means that it can’t be expressed in the form where and are integers and . The commonly used interpretation of () is also an approximation.
In this blog, we’ll learn how to estimate the value of using random numbers.
Let’s start with drawing a circle of radius around the origin as shown below.
Let’s place the circle in a square whose one side has the same length as that of the diameter of the circle.
The following illustration shows the circle inscribed in a square.
Guess what would be the length of each side of the square? As the length of each side is and , therefore the length of each side is .
Let’s compute the area of both shapes: the circle as well as the square. The area of the circle is . As in this setting, so, the area of this circle is . The area of a square is computed as the square of the length of its side. The length of one side of the square is and . Therefore, the length of one side is , and the area of the square is .
Assume that the square is the total surface area available to us and we hang a circular dartboard on this area. The geometric properties of the available area and the dartboard are the same as those of our drawn shapes. We want to throw a dart and hit the circle. There are two possibilities:
The probability of hitting the circle is the ratio of the area of the circle and that of the square. Let’s call it .
Now, let’s model and conduct an empirical experiment to throw the darts and examine whether they hit the circle or miss it. Each point hit by a dart is defined as . The exact values of and are random as they are being determined by the physical experiment. We need to generate two random numbers notating and . For this purpose, we use the rand()
function of C++. It generates a positive random integer. If we reanalyze our surface area, both of its coordinates are bound between and . Our randomly generated number should be a real number between and .
RAND_MAX
which represents a significantly large integer (greater than or equal to the random integer).RAND_MAX
so that we get a real number as the output. This confines the random number between and (both inclusive).We perform the experiment twice for and coordinates.
Let’s understand how we can characterize a randomly generated point geometrically. Pythagoras’ theorem states that the hypotenuse can be computed using the other two sides in a right-angled triangle. A right-angled triangle that has a hypotenuse equal to the radius of the circle is shown in the following figure. We drop a perpendicular to get the projection on the x-axis. The perpendicular height gives the height and the projection length gives the base of the right-angled triangle.
According to Pythagoras’ theorem:
The circle has . So, . The above equation becomes:
If our randomly generated point falls exactly at the boundary of the circle, then . If it falls within the circle then . And if it falls outside the circle then .
Let’s treat the outer square as a wall where we have hung a circular dartboard at its center. The radius of the circle is . If we throw a dart then it would either hit the circle or miss it. If the dart hits the circle then . If it misses the circle then it hits the part of the square that’s not covered by the circle. If we perform a physical experiment repeatedly—say 100,000 times and call it — we can count how many times the dart hits—let’s call it —the circle. Based on this empirical exercise, we can estimate the probability of hitting the circle as follows:
Recall we computed using the geometric properties too. We can equate equations and as follows:
In the following program, we simulate the same experiment. We generate 100,000 times, count , and estimate .
#include <iostream>using namespace std;int main(){srand(time(0));double trials = 100000;double hits = 0;for (int i = 0;i<=trials;i++){double x = -1 + 2 * (rand()*1.0)/RAND_MAX;double y = -1 + 2 * (rand()*1.0)/RAND_MAX;if (x*x+y*y<=1)hits++;}double pi = hits/trials*4;cout<<"Estimated value of pi = "<<pi;return 0;}
Let’s go through the code:
100000
in trials
and the number of hits as zero in hits
. We set their data type to double
so that we may preserve the calculations in the real domain later.trials
iterations.hits
. Otherwise we treat it as a missed shot and the next iteration follows.pi
based on the randomized experiment and geometric characteristics of the circle and square.Run this program multiple times and you will see a fluctuation in the estimated value of pi
each time you run it. This fluctuation will remain there because our estimation is based on the randomized experiment.
In this blog, we used a dartboard inscribed in a square to estimate the value of . We used geometric characteristics and empirical experiments to estimate the probability of hitting the dartboard when a dart is thrown. Equating the probability computed using these two methods led us to estimate the value of .
Consider exploring some of the programming-intensive courses at Educative:
Learn to Code: C++ for Absolute Beginners
C++ is a versatile language known for its efficiency and flexibility, widely used in industries like game development, finance, and system programming. This course dives deep into C++ programming foundations, focusing on practical problem-solving techniques. You will start by learning basic problem-solving skills using simple programs and execution sheets. Then, you'll explore decision-making, branching, loops, and manipulation of strings and arrays using C++ programming. Finally, the course will cover functions and complex program structures, ensuring a comprehensive grasp of the language's capabilities. By the end, you will be equipped with problem-solving skills, a solid understanding of C++ basics, and confidence in writing structured code, setting you on the path to becoming a proficient C++ developer.
Competitive Programming in C++: The Keys to Success
Competitive programming can be a great way to build out your programming skills, get on any major company’s radar, and earn a little extra cash along the way. In this course, you will learn to prepare for competitive programming contests like ACM ICPC, Google CodeJam, Facebook HackerCup, and many more. Each topic is broken down with a healthy mix of theory, code samples, step-by-step solved sample problems, illustrations, useful practice problems, and tips and tricks for faster implementation. You will need some solid C++ foundations coming into this course, but by the end it, you will be confident enough in your C++ skills to take home the win.
Learn C++: The Complete Course for Beginners
If you're a beginner and want to learn C++ to start your coding journey, you're in the right place. This comprehensive course starts from the absolute basics and gradually builds up to exciting real-life coding projects. The emphasis throughout is on practical lessons and analogies that you can relate to. As you learn, you'll work your way through dozens of coding exercises that you can run from right inside your browser. By the time you're done, you'll have a strong grasp of C++, one of the most in-demand programming languages in the world. You'll have gotten plenty of hands-on practice and will be able to confidently write your own applications.
Free Resources