DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Calculate Pi
This script presents a way to compute Pi using a statistic method.
See http://en.wikipedia.org/wiki/Computing_Ï€ for further algorithms.
import random, math
class calcPi:
def __init__(self):
self.times = pow(10,6)
self.i = 0
self.isnot = 0
def IsOnCircle(self,x,y):
if math.sqrt(x**2+y**2) < 1:
return True
else:
return False
def run(self):
for x in range(self.times):
x,y = random.random(),random.random()
if self.IsOnCircle(x,y):
self.i+=1
else:
self.isnot+=1
def getResults(self):
return (float(self.i), float(self.isnot))
def getPi(self):
self.run()
r = self.getResults()
return r[0]/(r[0]+r[1])*4
if __name__ == '__main__':
print calcPi().getPi()





