Skip to content

Commit e7dba2c

Browse files
Add get_random in walker scheme class
1 parent 1d5d49a commit e7dba2c

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/rewriting_hw_1/walker_scheme.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from math import floor
2+
3+
14
class WalkerScheme:
25
def __init__(self, events_and_probabilities: list[tuple[str, int]]):
36
if sum(probability[1] for probability in events_and_probabilities) != 1:
@@ -67,7 +70,7 @@ def _get_table(self, events_and_probabilities):
6770

6871
return table
6972

70-
71-
scheme = WalkerScheme([("A", 0.1), ("B", 0.5), ("C", 0.4)])
72-
73-
print(scheme.table)
73+
def get_random(self, probability):
74+
if self.table[floor(probability * len(self.table))][2] > probability:
75+
return self.table[floor(probability * len(self.table))][0]
76+
return self.table[floor(probability * len(self.table))][1]

0 commit comments

Comments
 (0)