Last active
May 3, 2020 02:09
-
-
Save colemilne54/e05e1837d60b6fd485cb64467fbe2402 to your computer and use it in GitHub Desktop.
Pi Derived From Random Numbers (Visualized)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://www.youtube.com/watch?v=pvimAM_SLic | |
# Algorithm code from video above, code past print statement is my original code. | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import random | |
def estimate_pi(n): | |
num_point_circle = 0 | |
num_point_total = 0 | |
xList = [] | |
yList = [] | |
for _ in range(n): | |
x = random.uniform(0, 1) | |
y = random.uniform(0, 1) | |
xList.append(x) | |
yList.append(y) | |
distance = x**2 + y**2 | |
if distance <= 1: | |
num_point_circle += 1 | |
num_point_total += 1 | |
print(4 * num_point_circle / num_point_total) | |
plt.figure() | |
plt.scatter(xList, yList, s=1) | |
plt.yticks(np.arange(0, 2, 1)) | |
plt.xticks(np.arange(0, 2, 1)) | |
ax = plt.gca() | |
circle = plt.Circle((0, 0), 1, color='black', fill=False) | |
ax.add_artist(circle) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment