Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

DZone's Guide to

# Calculate Pi

·
Free Resource

Comment (0)

Save
{{ articles[0].views | formatCount}} Views
```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()
``````
Topics:

Comment (0)

Save
{{ articles[0].views | formatCount}} Views

Opinions expressed by DZone contributors are their own.