Calculating 'pie' on the GPU using a pseudo probability function (estimating Pi using Monte Carlo method). This approach for estimating the value of pi using random numbers can illustrate the relationship between the sample size, the estimate of pi, and the error in the estimate. The Monte Carlo method generates random points inside a square and counts the number of points that fall inside a circle inscribed in the square to estimate pi.
First, let's start by understanding what pie actually is, the definition for pie is:
The number π is a mathematical constant that is the ratio of a circle's circumference to its diameter, approximately equal to 3.14159...
We're going to do a calculation using a unit circle. Instead of a whole circle which has a range of -1 to 1, we'll only do the calculation with a quarter (i.e., 0 to 1). A circle is the same - so any calculation on this small quadrant can be multipled by 4 to get the final answer.
Now we can generate lots of random numbers in the range 0 to 1 for the corner. Take a random point P at coordinate X, Y such that 0 <= x <= 1 and 0 <= y <= 1. If x2 + y2 <= 1, then the point is inside the quarter disk of radius 1, otherwise the point is outside.
Random points for the x and y (0 to 1) - how the points are distributed inside and outside the circle quarter.
Before implementing the calculation on the compute shader - we can do a small proof of concept in JavaScript to see that the number returned is in the ball-park (close to 3.14).
Visitor:
Copyright (c) 2002-2025 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.