How I calculated PI (π) from a PIZZA!

Abin Isaac
4 min readMay 4, 2022

A small adventure with “Random Numbers”

Say a random number….

I know you can say a lot of random numbers, but the question here is HOW ??

Look, random numbers which came out of my mind…789, 842, 556, 333 (Actu😂lly I had asked numbers btw 1 to 1000)


Before answering that, lets look into our AcTion Plan for this Ad⛰️Venture

Create random function using Python (No Library imports)

⭐⭐ Visualize and proving the random function using graphs ⭐⭐

⭐⭐⭐ Calculating the PI value using our random function ⭐⭐⭐ why only pseudo random ?? why can’t we get a real random number ?


All we can do is to increase the pseudo-ness in the NUMBERS. We think it to be random when we don’t know the real story. So real random number don’t exist, they are just some hidden stories. (dis😒gree?, Welcome to comments)

Here, we are using some mathematical formula. It may be different in case of Atmospheric noise, magnetic field fluctuations etc (considered more real).

Lot of dices in water are moved with two small fans to create more real random numbers for security.
credit: Real

We will be choosing LCG algo to complete this Journey. Its just a way to create random numbers.

LCG stands for Linear Congruential Generator

Come, meet our four new friends who are going to guide us in this dangerous adventure 🌳🌲🦁🌳 🕳 🐯🌴🕳️🥦🕳🐍⛰️🌴🐂 🥦🌲🥦⛰️

a is the multiplier, c stands for increment and m stands for the modulo

seed is the start point and which gets updated with time.

seed and modulo are the starting and the ending point respectively for the range of generated random numbers.

seed = ( a x seed + c ) % m

We will use small values for a, c, m, seed and create a function to generate random number.

Random Number Generator
for loop to generate 20 random numbers

This is not a good random number generator as the random numbers repeat after 6 values. We need to increase the range to increase the randomness.

a, m, c values are increased

Yay!!! Now there is no repetition 🎉🎉

We have reached our first goal as we have successfully created a random number generating function. Now going for the remaining goals__🚶_🏳️

Now, we will visualize values from function to prove uniformity in the random numbers…🎯

1 D matrix of 1 Lakh random numbers using Numpy
Visualization using Matplotlib

Wow!!! They all are uniformly distributed🎉🎉

Now we have our 🏳️INAL G🎯AL remaining… Getting the value of PI using our function….Come lets run to it !

I know you all have got tired 😩 due to this long journey so lets take a break….

Come, Take a piece of pizza 😂

Break time over…F😂cus on Dot part only !

Formula to find π 🤔

Here we have two shapes a 1/4th Circle and a Square where:

radius of circle = side of square = 1 cm

Area of square r x r = 1 x 1 = 1 and Area of 1/4th circle = π ÷ 4

π = 4 x Area of 1/4th Circle ÷ Area of Square

If we calculate the area by the number of dots then

lets take Total points in Square is 1 Lakh and

now we need to find the Total dots inside the Quarter Circle so we can get the π

Here we have generated 1 lakh random points and the points which resided inside the circle were separated using x**2 + y**2 ≤ 1.

….. we used our formula to find the Value of π correct to 2 decimal points…..🎉🏳️🎯

Great! We have completed our journey !!

Thank you so much for staying there 👋

👉 Colab link for the code 🎁

If you really enjoyed please do give a clap !