Created
October 26, 2020 13:19
-
-
Save sXakil/1e75831ac90e02cb441c3732856dc773 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from math import sqrt | |
from random import random | |
lim = 15 # int(input("Number of simulation: ")) | |
radius = 1.0 # float(input("Enter the radius: ")); | |
partition = 4 #{'full': 1, 'half': 2, 'quadrant': 4}[input("Enter the partition (full, half or quadrant): ")] | |
R1 = [round(random() * radius, 2) for i in range(0, lim)] | |
R2 = [round(random() * radius, 2) for i in range(0, lim)] | |
def getR2Distance(r1, r = radius): | |
return round(sqrt(abs(r - r1**2)), 3) | |
sqrtR2 = [getR2Distance(r1) for r1 in R1] | |
inOut = ['In' if R2[i] < sqrtR2[i] else 'Out' for i in range(0, lim)] | |
print() | |
print('R1'.rjust(6) + 'R2'.rjust(6) | |
+ 'sqrt(1-R1^2)'.rjust(14) + 'In/Out'.rjust(8) | |
+ 'M'.rjust(5) + 'N'.rjust(5)) | |
print('-'*44) | |
M = 0 | |
for i in range(0, lim): | |
M = len([1 for i in inOut[:i+1] if i == 'In']) | |
print(str(R1[i]).rjust(6) + str(R2[i]).rjust(6) | |
+ str(sqrtR2[i]).rjust(14) + inOut[i].rjust(8) | |
+ str(M).rjust(5) + str(i+1).rjust(5)) | |
PI = round((M * partition) / lim, 3) | |
print(f"\n\nThe predicted value of PI is {PI}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment