-
Notifications
You must be signed in to change notification settings - Fork 0
/
rand.py
33 lines (25 loc) · 839 Bytes
/
rand.py
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
import random
def chance(x, y=1):
'''Has a chance x out of y to return True.'''
return random.random() <= x
# I just like this name better.
choose = random.choice
# this class is a bit odd and could be put to work better
class Odds:
'''
A class that takes a list of (identifiers, weight) tuples
and uses them to construct a probability switch.
'''
def __init__(self, oddsList):
self.oddsMap = {}
totalChance = sum([b for [a, b] in oddsList])
s = 0
for (key, weight) in oddsList:
low = s
s += weight
self.oddsMap[key] = (low / totalChance, s / totalChance)
def roll(self):
self.r = random.random()
def test(self, key):
(low, high) = self.oddsMap[key]
return self.r <= high and (self.r > low or low == 0)