-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroll.py
70 lines (59 loc) · 1.78 KB
/
roll.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from random import choice
def expand(table):
"""Flatten and expand the table.
The original table is a list of tuples, of which the 1st element is a
value and the second a weight. The resulting table is a list of values,
where each value appears a number of times defined by its weight.
:param table: Original table
:type table: List of (Value, Weight).
:return: Flattened table
:rtype: List of values
"""
t = []
for r in table:
for _ in range(r[1]):
try:
t.append((r[0], r[2]))
except IndexError:
t.append((r[0], None))
return t
def roll(table):
"""Roll the table.
Return a random value from the table, with probabilities dependent on each
value's weigth.
:param table: [description]
:type table: [type]
:return: [description]
:rtype: [type]
"""
if table[0].__class__ == tuple:
t = expand(table)
else:
t = table
rolled = choice(t)
if rolled[1] is None:
return rolled[0]
else:
return rolled
def roll_exclusive(table1, table2):
"""Roll each table seperately, without yielding the same index from each.
Return a random value from each table, while ensuring that they don't
share the same index number from their respective table.
:param table1: [description]
:type table1: [type]
:param table2: [description]
:type table2: [type]
:return: [description]
:rtype: [type]
"""
t1 = roll(table1)
for index1, entry in enumerate(table1):
if entry[0] == t1:
break
while(True):
t2 = roll(table2)
for index2, entry in enumerate(table2):
if entry[0] == t2:
break
if index1 != index2:
return (t1, t2)