Checking an answer by simulation
Many quant coding rounds start with a probability puzzle and end with "now write code to check it." The recipe is the same every time: write one function that plays out a single trial and returns a result, run it times, and average.
Take two independent uniforms on and ask for . If the product is always below . If you need . So
import random, math
def trial():
return random.random() * random.random() < 0.5
random.seed(1)
n = 100_000
p = sum(trial() for _ in range(n)) / n
se = math.sqrt(p * (1 - p) / n)
print(f"{p:.4f} +/- {1.96 * se:.4f}") # 0.8469 +/- 0.0022The estimate is only useful with its error bar. A proportion estimated from independent trials has standard error , which is the from the Monte Carlo Option Pricing lesson with . Here that is , so a 95% band is about , and the exact sits inside it. A claimed answer of looks close, but it is about 12 standard errors below the estimate, so the simulation rules it out.
The error shrinks like : four times the trials halves it. Cost is times the cost of one trial. In an interview, say both numbers out loud ("100k trials gives me about three decimal places") and fix the seed so the run is reproducible.