In [1]:

```
from random import choices
from collections import Counter
```

In [2]:

```
# Define a 6-sided die
faces = list(range(1, 7))
faces
```

Out[2]:

In [3]:

```
# A function for rolling a pair of dice
roll_2_dice = (lambda: choices(population=faces, weights=None, k=2))
pair = roll_2_dice()
pair
```

Out[3]:

In [4]:

```
sum(pair)
```

Out[4]:

In [5]:

```
# Simulate rolling a pair many times and tracking the outcomes
rolls = [sum(roll_2_dice()) for _ in range(50_000)]
#print(rolls)
```

In [6]:

```
Counter?
```

In [7]:

```
# Count those outcomes (number of sums of paired elements)
rolls_counts = Counter(rolls)
print(rolls_counts)
```

In [8]:

```
# Sort by faces [0] or counts [1] and store the sorted results as a dictionary
rolls_counts = dict(sorted(rolls_counts.items(), key=lambda x: x[0], reverse=True))
print(rolls_counts)
```

In [9]:

```
# Plot the outcome of simulating many dice rolling (Binomial Distribution)
import matplotlib.pyplot as plt
%matplotlib inline
labels, values = zip(*rolls_counts.items()) # Unpack dict
plt.bar(x=labels, height=values);
```

In [10]:

```
# If you roll two dice, how likely is it that your sum is greater than 7
sum(v for k, v in rolls_counts.items() if k > 7) / sum(rolls_counts.values())
```

Out[10]:

In [11]:

```
def duration(seconds):
'''
Encodes seconds into a formated string d:h:m:s.
'''
time = seconds
day = time // (24 * 3600)
time = time % (24 * 3600)
hour = time // 3600
time %= 3600
minutes = time // 60
time %= 60
seconds = time
print("d:h:m:s-> %d:%d:%d:%d" % (day, hour, minutes, seconds))
```

In [12]:

```
# If you roll seven dice, how likely is it that your sum is greater than 35?
import time
start = time.time()
faces = list(range(1, 7))
roll_7_dice = (lambda: choices(population=faces, weights=None, k=7))
rolls7 = [sum(roll_7_dice()) for _ in range(400_000_000)]
rolls_counts7 = Counter(rolls7)
likelihood = sum(v for k, v in rolls_counts7.items() if k > 35) / sum(rolls_counts7.values())
print("The likelihood is {}".format(likelihood))
duration(time.time() - start)
```

In [ ]:

```
```

In [ ]:

```
```

In [ ]:

```
```

In [ ]:

```
from random import choices
from collections import Counter
# 6-sided die
faces = list(range(1, 7))
# A function for rolling a pair of dice
roll_2_dice = (lambda: choices(population=faces, weights=None, k=2))
# Simulate rolling a pair many times and tracking the outcomes
rolls = [sum(roll_2_dice()) for _ in range(50_000)]
# Count those outcomes
rolls_counts = Counter(rolls)
# If you roll two dice, how likely is it that your sum is greater than 7
sum(v for k, v in rolls_counts.items() if k > 7) / sum(rolls_counts.values())
```

In [ ]:

```
```

In [ ]:

```
```

In [ ]:

```
```

In [ ]:

```
```