How to find the value of an un-integrable integral: Monte Carlo Integration [md]
Created: Sun Jul 12 2026
Introduction
Imagine, for a second, an arbitrary function . We don’t have access to or its parameters themselves, but given an input , we can get the value of . A blackbox, so to speak. Now, how can one integrate ? Monte Carlo Integration is one way to numerically 1 solve this problem. But what is Monte Carlo Integration in the first place?
The Law of Large Numbers
First, lets look at a mathematical law that induces our method - the law of large numbers states that given samples of a function from independent and identically distributed values, the sample mean converges to the true mean (or true value). That is,
Now this might sound a bit confusing, so let’s consider an example, take
that is, a normal distribution centered on with a standard deviation of . The true mean of the function is . Now, the law of large numbers states that, given independent (obtaining a random number does not affect obtaining the next one) and identically distributed (all are obtained from the same distribution) samples 2, that
In the widget, you can perform a monte carlo simulation. Note that as we generate more samples, the sample mean converges to the true mean. I will expand on this more below.
That is, if we find the average of infinite random samples of the function , we will obtain the true mean, . Note that this can be accomplished even if the function is a blackbox, since we only need the ability to find the value of the function at random points, no access to the parameters themselves.
Towards Monte Carlo Integration
Now, once again, let’s look at the function . Lets say we define a probability domain for each . Then, we can find the expected value of this function as -
Where is the domain of (For example, if is a random number between and , then ).
But we know from the law of large numbers that
So, we can see that
Now, consider another function, that we define as
Then, we can once again say that
So, we can find the value of the integral we wanted, simply by sampling the function at a large number of points.
An example of Monte Carlo Integration
Let’s once again look at an example to understand this better. Let’s attempt to find the area under the graph , between and . That is, we want to find -
First, we must select a pdf for . Lets choose the uniform distribution, since our domain is ,
From our derivation, we have
Lets try to take a few samples, and understand what this means graphically. Here, I have set and . Next, I took 4 samples and plotted the value of the function at those samples. If you note the second-to-last equation, you will see that we are calculating
This is merely the averagea of the areas of all rectangles with base and height for all our samples. This is easier visualized in Figure-1.

This means that, if we average the areas of the rectangles subtended by an infinite number of samples, we will approach the true value of the integral. But what is the benefit to using this technique? The answer lies in how this approximation approaches the true value of the integral.
Convergence, or, why we need a large number of samples
Now, we have established previously, that from the law of large numbers, we can derive that -
But note the instead of a . So, how much can we trust this estimate of the integral?
First, we need to discuss some things about functions that rely on randomness. If you are familiar with probability theory, you would be familiar with , the variance of a random variable. This is a measure that dictates how much a random variable “deviates from the norm” - how all-over the place it is. Now, this measure is defined as -
That is, its the average squared error of the function. This measure has 2 properties we will use in our further proofs -
where is any scalar constant, and
where and are independent functions.
Now, we can define how much trust we have in our estimate of the integral by finding out its variance - the lower the variance, the closer it is to the true value, and vice versa. Lets find this value -
we want to find
This derives from the first rule about variance we discussed above, next -
This derives from the second rule discussed above, since is independent (does not depend on previous values of itself), and since is also independent. Finally, we can say that since for any given , and are identical (not numerically), but since are all drawn from the same distribution, so the randomness only depends on , we can say that the variance of all samples, would be the same, so we can claim that -
So, as we can see, the variance of our estimate, is proportional to . This means that if we increase our number of samples, it makes our estimate better.
This is also true intuitively, as in the case discussed above of finding the true mean of a normal distribution, as we generate more samples, we get closer and closer to the average becoming the true mean, .
Once again, this may be better understood visually, lets look at an example.
Visualizing the efficacy of a Monte Carlo Method
Let’s look at a function that we can integrate easily -
First, lets find the answer analytically -
Now, lets apply the monte carlo method.
First, we take a probability distribution,
Now, Lets sample using this probability distribution and use monte carlo integration -
Lets do this, and plot it out, as well as the resulting variance. In the widget below, the graph on top plots the estimate of the integral, overlaid on the true value. The bottom graph plots the variance of the estimation.
Clearly, the estimate approximates the true value, . Furthermore, the estimate variance also drops, following the rule of .
Conclusion
As we can see, using Monte Carlo Integration, we can solve integrals numerically that are impossible to solve analytically, such as . It is extremely powerful, and find many uses. While I did not entirely explain exactly what the Monte Carlo method is (and it’s alternatives), I hope you understood what Monte Carlo Integration is.
The Monte Carlo method induces many algorithms that make use of random numbers. Monte Carlo integration is just one of them. While it might seem a bit abstract to introduce these now, Monte Carlo integration is the core algorithm used in all path tracers3. Using computers capable of processing millions of samples in a second, it is plain to see that Monte Carlo Integration can be extensively used.
I personally like the elegance of understanding monte carlo integration simply to be averaging the areas of rectangles subtended by random samples, and that is why I wrote this article about it. I also intend to continue these articles until I satisfactorily explain how they are relevant to pathtracing.