Search code examples
pythonlistprobability

Determining the odds of each roll of dice


When playing games where you have to roll two die, it is nice to know the odds of each roll. For instance, the odds of rolling a 12 are about 3%, and the odds of rolling a 7 are about 17%.

You can compute these mathematically, but if you don’t know the math, you can write a program to do it. To do this, your program should simulate rolling two dice about 10,000 times and compute and print out the percentage of rolls that come out to be 2, 3, 4, . . . , 12.

State Space for rolling 2 dice

First my problem comes from the probability percentage. Considering only six can give twelve in a state space of 36 possibilities how comes the probability is 3?

Because of this i have been unable to complete my program. Below is my attempted solution

from random import randint
dice_roll=[]
outcome =(2,3,4,5,6,7,8,9,10,11,12)
sim =10

for simulations in range(sim):
    first_dice_roll = randint(1,6)
    second_dice_roll = randint(1,6)

    dice_roll.append(first_dice_roll + second_dice_roll)
    sumi = sum(dice_roll)
print(dice_roll,"Dice roll")

Solution

  • My solution:

    from random import randint
    
    probs = {2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0,10:0,11:0,12:0}
    
    for i in range(10000):
        diceTotal = randint(1,6)+randint(1,6)
        
        probs[diceTotal] += 1
    
    for key,value in probs.items():
        print(str(key) + " => " + str(value/100) + "%")
    
    Each possible total is a key in a dictionary and its value is incremented whenever that total is the result of the dice rolling.
    
    

    Output:

    2 => 2.8%
    3 => 5.64%
    4 => 7.96%
    5 => 11.44%
    6 => 13.68%
    7 => 16.42%
    8 => 13.81%
    9 => 11.47%
    10 => 8.55%
    11 => 5.54%
    12 => 2.69%
    

    The results are quite close to the theoric probabilities. Increasing the number of dice rolls would of course improve the estimation.