-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpi_game.py
More file actions
43 lines (32 loc) · 931 Bytes
/
pi_game.py
File metadata and controls
43 lines (32 loc) · 931 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import math
from random import uniform
def random_coords():
return (uniform(-1, +1), uniform(-1, +1))
class PiGame(object):
def __init__(self):
self.m_total = 0
self.m_inside = 0
self.throw_coords()
def add(self):
self.m_total = self.m_total + 1
if self.inside():
self.m_inside = self.m_inside + 1
def value(self):
try:
return float(self.m_inside) / self.m_total * 4.0
except BaseException:
return float('nan')
def error(self):
return abs(math.pi - self.value()) / math.pi * 100.0
def throw_coords(self):
self.m_coords = random_coords()
return self.m_coords
def coords(self):
return self.m_coords
def inside(self):
x = self.m_coords[0]
y = self.m_coords[1]
if x**2 + y**2 < 1:
return True
else:
return False