# Dice Project

Last updated: May 22nd, 2020

This is the final project of Intro to Probability (Part 1)

In [57]:
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:

In [ ]:


In [58]:
def dice():
return random.randint(1,6)

In [59]:
dice()

Out[59]:
6

Suppose that you attack your enemy, to do this you will throw a certain number of dice. The attack will be the sum of your throw. Define a function "attack" to calculate this amount depending on the number of dice.

In [ ]:


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

In [61]:
attack(3)

Out[61]:
15

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

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

Define a function "battle" that depends on this two values ("enemy_armour" and "number_of_dice") and returns true if you win the battle (if there is a tie, you lose) and false otherwise.

In [ ]:


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

In [63]:
battle(4,18)

Out[63]:
False

Let's try some specific battles. Suppose that the enemy has an armour with power 8 and you attack with 2 dice. How is the battle? Define a function "battle_1" to model this battle.

In [ ]:


In [64]:
def battle_1():
return battle(2,8)


Now, what if the enemy has an armour with power 12 and you attack with 3 dice? Define a function "battle_2" to model this one.

In [ ]:


In [65]:
def battle_2():
return battle(3,12)


From the two previous battles, which one will be more likely for you to win? Let's try to calculate the empirical probability of winning each.

To do so, let's define a function "empiric_prob" that receives an experiment (in this case we want to use battle_1 and battle_2 as our experiments) and a number of trials "n" (with a default value of 10000), and returns the empirical probability of winning that experiment.

In [ ]:


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

In [75]:
empiric_prob(battle_1)

Out[75]:
0.275
In [76]:
empiric_prob(battle_2)

Out[76]:
0.255

So, according to the empirical probability we are more likely to win battle_1. But it is a close call, could it be that the number of trials is influencing the results?

To be sure, let's compare the empirical probability with the theoretical:

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

Let's calculate how many of all the 36 possible outcomes are grater than 8.

In [ ]:


In [74]:
amount = 0
for x in range(1,7):
for y in range(1,7):
if x+y>8:
amount=amount+1
print(amount)

10


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

In [76]:
5/18

Out[76]:
0.2777777777777778

If we compare this amount with the empirical probability, the difference is quite low.

In [77]:
abs(empiric_prob(battle_1)-5/18)

Out[77]:
0.006077777777777793

It's close to "empiric_prob(battle_1)", but it could be better. What could we change to make the empirical probability a better approximation?

In [79]:
abs(empiric_prob(battle_1,100000)- 5/18)

Out[79]:
0.0005277777777777937

We can do a table to compare the results with different values for n:

In [80]:
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


Out[80]:
Times Empirical Probability Difference
0 10000 0.270700 0.007078
1 100000 0.276150 0.001628
2 1000000 0.277570 0.000208
3 10000000 0.277962 0.000184

Try to do the same comparison with battle_2

In [ ]:


In [ ]:
amount = 0
for x in range(1,7):
for y in range(1,7):
for z in range(1,7):
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


In [ ]:


In [ ]:


In [ ]: