This is the final project of Intro to Probability Part 1

```
import random
import pandas as pd
```

Imagine that we will play a role-playing game. First, we need to know how throw a dice.

Try to do alone:

```
```

```
def dice():
return random.randint(1,6)
dice()
```

Suppose that you attack your enemy. Define a code to attack that depend on the amount of dice.

```
```

```
def attack(number_of_dice):
return sum([dice() for _ in range(number_of_dice)])
```

```
attack(3)
```

Now that we know how to attack, we begin a battle.

We call "enemy_armour" to number that represent the power of the armour of your enemy and "number_of_dice" to the amount of dice when attack.

Define a battle that return true if you win the battle (the sum of the dice>enemy armour) and false in otherwise.

```
```

```
def battle(number_of_dice,enemy_armour):
'''Returns True if you beat the enemy and False otherwise'''
return attack(number_of_dice)>enemy_armour
```

Suppose that the enemy has an armour with power 8 and you attack with 2 dice. How is the battle?

```
```

```
def battle_1():
return battle(2,8)
```

and if the enemy has an armour with power 12 and you attack with 3 dice?

```
```

```
def battle_2():
return battle(3,12)
```

What previous battle will more likely than you win? We try to calculate the empirical probability.

```
```

```
def empiric_prob(experiment,n=10000):
return sum([experiment() for _ in range(n)])/n
```

```
empiric_prob(battle_1)
```

```
empiric_prob(battle_2)
```

We will compare the empirical probability with the theoretical:

We begin with the battle of 2 dice and 8 in the enemy armour. Then we need that sum of the dice is more than 8.

```
amount = 0
for x in range(1,7,1):
for y in range(1,7,1):
if x+y>8:
amount=amount+1
print(amount)
```

Then we have $\frac{10}{36}=\frac{5}{18}$, that it this is:

```
5/18
```

```
abs(empiric_prob(battle_1)-5/18)
```

Is close to "empiric_prob(battle_2)", but it can be better. What will we can do to better the empirical probability?

```
emp_prob = empiric_prob(battle_1,n=100000)
emp_prob
```

```
abs(emp_prob- 5/18)
```

We can do a table to compare the result with different n:

```
times = [10000, 100000, 1000000, 10000000]
experiments = []
for t in times:
experiments.append(empiric_prob(battle_1,t))
dif = []
for i in range(len(times)):
dif.append(abs(experiments[i]-5/18))
data = {'Times':['10000', '100000', '1000000', '10000000'], 'Empirical Probability': experiments,
'Difference': dif}
df = pd.DataFrame(data)
df
```

Try to do the compare in the battle 2

```
```

```
amount = 0
for x in range(1,7,1):
for y in range(1,7,1):
for z in range(1,7,1):
if x+y+z>12:
amount=amount+1
times = [10000, 100000, 1000000, 10000000]
experiments = []
for t in times:
experiments.append(empiric_prob(battle_2,t))
dif = []
for i in range(len(times)):
dif.append(abs(experiments[i]-amount/6**3))
data = {'Times':['10000', '100000', '1000000', '10000000'], 'Empirical Probability': experiments,
'Difference': dif}
df = pd.DataFrame(data)
df
```

```
```

```
```

```
```