Jed Rembold
March 30, 2026
T when we fit Planck’s Law, for
instanceSuppose we wanted to evaluate the area under the curve \[ f(x) = -\frac{4}{5}x + 4 \] from \(x=0\) to \(x=5\), using Monte Carlo methods. This describes a triangle with height 4 and width 5, and thus should have an area of 10. Is that what we get?
What if we slightly complicate this function by looking instead at a piece-wise function: \[ f(x) = \begin{cases} 0 & 0 \leq x < 2; \\ -\frac{4}{5}x + 3.2 & 2 \leq x \leq 4; \\ 0 & 4 < x \leq 5 \end{cases} \] This now describes a triangle with height of 1.6 and width of 2, thus having an area of 1.6. Does the Monte Carlo method still work?

Selecting discrete values from a continuous probability distribution often utilizes Rejection Sampling
One implementation, in Python, might look like:
def sample_from_prob_dist(pdf, lower, upper, num):
samples = np.zeros(num)
count = 0
while count < num:
x = np.random.uniform(lower, upper)
if np.random.uniform(0,1) <= pdf(x):
samples[count] = x
count += 1
return samplessamp <- function(pdf, lower, upper, num) {
samples <- numeric(num)
count <- 0
while (count < num) {
x <- runif(1, lower, upper)
if (runif(1, 0, 1) <= pdf(x)) {
count <- count + 1
samples[count] <- x
}
}
return(samples)
}
Now suppose we wanted to evaluate the area under the same piece-wise function as earlier: \[ f(x) = \begin{cases} 0 & x < 2; \\ -\frac{4}{5}x + 3.2 & 2 \leq x \leq 4; \\ 0 & x > 4 \end{cases} \] But this time choosing points based on two different probability distributions: \[ {p(x) = \begin{cases} 0 & x < 2; \\ 0.5 & 2 \leq x \leq 4; \\ 0 & x > 4 \end{cases}}\qquad {p(x) = \begin{cases} 0 & x < 2; \\ \frac{1}{2}\left(x - 2\right) & 2 \leq x \leq 4; \\ 0 & x > 4 \end{cases}} \]