Imagine a square in the plane from coordinates (-1, -1) to (1,1), and also imagine a circle having radius 1 and origin in (0,0).
Generate a random number x in interval (-1, 1), and a random number y also in (-1, 1). What is the probability that (x, y) is inside the circle?
I hope you agree that the probability is the area of the circle over the area of the square: .
This means that if we randomly generate many points inside the square the number of the points inside the circle will be multiplied by the total number of points generated.
Based on this idea, following C code will compute an approximation of :