# Dice Exercises

forked from Aylenml/test

Last updated: September 8th, 2020

This is the final project of Intro to Probability Part 1

In :
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 :
def dice():
return random.randint(1,6)
dice()

Out:
6

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

In [ ]:


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

In :
attack(3)

Out:
15

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.

In [ ]:


In :
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?

In [ ]:


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


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

In [ ]:


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


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

In [ ]:


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

In :
empiric_prob(battle_1)

Out:
0.2781
In :
empiric_prob(battle_2)

Out:
0.2584

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.

In :
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)

10


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

In :
5/18

Out:
0.2777777777777778
In :
abs(empiric_prob(battle_1)-5/18)

Out:
0.002922222222222215

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

In :
emp_prob = empiric_prob(battle_1,n=100000)
emp_prob

Out:
0.27883
In :
abs(emp_prob- 5/18)

Out:
0.0010522222222222322

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

In :
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:
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 compare in the battle 2

In [ ]:


In :
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


Out:
Times Empirical Probability Difference
0 10000 0.258000 0.001259
1 100000 0.256750 0.002509
2 1000000 0.259447 0.000188
3 10000000 0.259383 0.000124
In [ ]:


In [ ]:


In [ ]: