large part of the Monte Carlo literature is dedicated in developing strategies to improve the error estimates. In particular, stratified sampling—dividing the region in sub-domains—and importance sampling—sampling from non-uniform distributions—are two examples of such techniques. Example A paradigmatic example of a Monte Carlo integration is the estimation of π. Consider the function and the set Ω = [−1,1] × [−1,1] with V = 4. Notice that Thus, a crude way of calculating the value of π with Monte Carlo integration is to pick N random numbers on Ω and compute In the figure on the right, the relative error is measured as a function of N, confirming the . C example Keep in mind that a true random number generator should be used. int i, throws = 99999, insideCircle = 0; double randX, randY, pi; srand(time(NULL)); for (i = 0; i < throws; ++i) { randX = rand() / (double) RAND_MAX; randY = rand() / (double) RAND_MAX; if (randX * randX + randY * randY < 1) ++insideCircle; } pi = 4.0 * insideCircle / throws; Python example Made in Python. from numpy import random import numpy as np throws = 2000 inside_circle = 0 i = 0 radius = 1 while i < throws: # Choose random X and Y centered around 0,0 x = random.uniform(-radius, radius) y = random.uniform(-radius, radius) # If the point is inside circle, increase variable if x**2 + y**2 <= radius**2: inside_circle += 1 i += 1 # Calculate area and print; should be closer to Pi with increasing number of throws area = (((2 * radius) ** 2) * inside_circle) / throws print(area) Wolfram Mathematica example The code below describes a process of integrating the function from using the Monte-Carlo method in Mathematica: func[x_] := 1/(1 + Sinh[2*x]*(Log[x])^2); (*Sample from truncated normal distribution to speed up convergence*) Distrib[x_, average_, var_] := PDF[NormalDistribution[average, var], 1.1*x - 0.1]; n = 10; RV = RandomVariate[TruncatedDistribution[{0.8, 3}, NormalDistribution[1, 0.399]], n]; Int = 1/n Total[func[RV]/Distrib[RV, 1, 0.399]]*Integrate[Distrib[x, 1, 0.399], {x, 0.8, 3}] NIntegrate[func[x], {x, 0.8, 3}] (*Compare with real answer*) Recursive stratified sampling Recursive stratified sampling is a generalization of one-dimensional adaptive quadratures to multi-dimensional integrals. On each recursion step the integral and the error are estimated using a plain Monte Carlo algorithm. If the error estimate is larger than the required accuracy the integration volume is divided into sub-volumes and the procedure is recursively applied to sub-volumes.