5.5b Monte Carlo integration labs
Source: Lab ideas from Computational Physics (Landau, Páez, Bordeianu), §5.5. Rewritten as original drills. Continues 5.5 Monte Carlo integration.
Estimate by stone throwing and by mean-value Monte Carlo. Graded randomness uses the chapter LCG only — not random.random().
r ← (1103515245 * r + 12345) % 2**31, then uniform r / 2**31. Default seed is 1 unless the prompt says otherwise. Advance the state before each draw (seed itself is not a sample).
Formula refresh
Warm up: pick the matching equation among lookalikes (same walk as the theory lesson).
Labs
Draw points uniformly in with the chapter LCG (seed ). Count hits with . Print
4 * hits / N
with 6 decimals.
No input. Use .
Write-ups
Using “fraction”, “area”, and “box”, explain:
In one sentence: how does stone throwing estimate a pond’s area?
Minimum 3 words. Your answer must include: “fraction”, “area”, “box”.
Meet the writing requirements, then you can open the model answer.
Explorers
# mc_stone.py — chapter LCG stone throw (original sketch)
M = 2**31
def rnd(r):
r = (1103515245 * r + 12345) % M
return r, r / M
def pi_est(N, seed=1):
r, hits = seed, 0
for _ in range(N):
r, u = rnd(r)
r, v = rnd(r)
x, y = 2 * u - 1, 2 * v - 1
if x * x + y * y <= 1:
hits += 1
return 4 * hits / N