Skip to content

Instantly share code, notes, and snippets.

@vndee
Created November 1, 2024 13:44
Show Gist options
  • Save vndee/7c309f0eb89eed8b79ec97faa9f7e9ed to your computer and use it in GitHub Desktop.
Save vndee/7c309f0eb89eed8b79ec97faa9f7e9ed to your computer and use it in GitHub Desktop.
def estimate_pi(rng, n=1000000):
inside = 0
for _ in range(n):
x = uniform(rng, -1, 1)
y = uniform(rng, -1, 1)
if x*x + y*y <= 1:
inside += 1
return 4 * inside / n
mt = MersenneTwister(42)
print(f"π ≈ {estimate_pi(mt)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment