-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomizer.py
More file actions
32 lines (24 loc) · 730 Bytes
/
Copy pathRandomizer.py
File metadata and controls
32 lines (24 loc) · 730 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
import random
class Weighted:
def __init__(self):
self.items = {}
self.total_weight = 0
self._index = 0
def append(self, key, weight):
self.items[self._index] = (key, weight)
self.total_weight += weight
self._index += 1
def roll(self):
r = random.randrange(self.total_weight)
cur_weight = 0
for item, weight in self.items.values():
cur_weight += weight
if r < cur_weight:
return item
raise Exception('Something is wrong!')
def count(self):
return len(self.items)
def Bool(chance=0.2):
r = random.randint(0, 100)
max_val = 100 * chance
return r < max_val