PhD Student working on Particle Physics Simulation using Monte Carlo Techniques
Monte Carlo methods are a class of computational algorithms that rely on repeated random sampling to obtain numerical results. They are particularly useful when dealing with problems that are difficult to solve analytically.
Here’s a basic example of Monte Carlo integration to estimate π:
import random
def estimate_pi(n_samples):
inside_circle = 0
for _ in range(n_samples):
x = random.uniform(-1, 1)
y = random.uniform(-1, 1)
if x*x + y*y <= 1:
inside_circle += 1
return 4 * inside_circle / n_samples
# Estimate π with 1,000,000 samples
pi_estimate = estimate_pi(1000000)
print(f"Estimated π: {pi_estimate}")