Monte Carlo methods are a class of computational algorithms, that rely on repeated random sampling. They can provide solutions to problems, that are otherwise untraceable or too complex to analyse mathematically.

Implementation

it is important not to overthink it, monte carlo simulations are very straight forward.
import numpy as np

def coinflip():
    return np.random.randint(0,2)

def monte_carlo(n):
    probabilities = []
    avg_result = 0
    for i in range(n):
        flip_result = coinflip()
        avg_result += flip_result
        prob_value = avg_result/(i+1)
        probabilities.append(prob_value)
    
    avg_result /= n

    return avg_result, probabilities

Pasted image 20250703140643.png