import numpy as np, matplotlib.pyplot as plt, sympy as sp, random
print(f"๐ Happy Pi Day! ๐\nฯ ≈ 3.{str(sp.N(sp.pi, 12))[2:]}")
def monte_carlo_pi(n=5000):
inside = sum(1 for _ in range(n) if (x:=random.random())**2
+ (y:=random.random())**2 <= 1)
plt.scatter(np.random.rand(n), np.random.rand(n),
c=['blue' if x**2 + y**2 <= 1 else 'red' for x, y
in zip(np.random.rand(n), np.random.rand(n))], s=1)
plt.title(f"Monte Carlo ฯ ≈ {4 * inside / n:.5f}"), plt.show()
monte_carlo_pi()
#source code --> clcoding.com
0 Comments:
Post a Comment