Dice Exercises

forked from Aylenml/test
Last updated: May 17th, 20202020-05-17Project preview

This is the final project of Intro to Probability Part 1

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

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

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

In [ ]:
 
In [3]:
def attack(number_of_dice):
    return sum([dice() for _ in range(number_of_dice)])
In [4]:
attack(3)
Out[4]:
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 [69]:
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 [70]:
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 [71]:
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 [72]:
def empiric_prob(experiment,n=10000):
    return sum([experiment() for _ in range(n)])/n
In [73]:
empiric_prob(battle_1)
Out[73]:
0.2781
In [74]:
empiric_prob(battle_2)
Out[74]:
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 [75]:
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 [76]:
5/18
Out[76]:
0.2777777777777778
In [77]:
abs(empiric_prob(battle_1)-5/18)
Out[77]:
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 [78]:
emp_prob = empiric_prob(battle_1,n=100000)
emp_prob
Out[78]:
0.27883
In [79]:
abs(emp_prob- 5/18)
Out[79]:
0.0010522222222222322

We can do a table to compare the result with different 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 compare in the battle 2

In [ ]:
 
In [83]:
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[83]:
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 [ ]:
 
Notebooks AI
Notebooks AI Profile20060