Skip to content

Instantly share code, notes, and snippets.

@Ian729
Created January 17, 2022 12:26
Show Gist options
  • Save Ian729/f7d8ed063d50901446fa167a721347ec to your computer and use it in GitHub Desktop.
Save Ian729/f7d8ed063d50901446fa167a721347ec to your computer and use it in GitHub Desktop.
Monte Carlo Pi
import random
# number of all points in 1/4 circle / number of all points = pi/4
def pi(n): return sum( [random.random()**2 + random.random()**2 <= 1 for _ in range(n)] ) / n * 4
# % python3 -i pi.py
print(pi(100))
# 3.52
print(pi(1000))
# 3.104
print(pi(10000))
# 3.1448
print(pi(100000))
# 3.13572
# print(pi(1000000))
# 3.14102
# print(pi(10000000))
# 3.1422764
# print(pi(100000000))
# 3.14178852
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment