Determining the odds of each roll of dice

Viewed 1795

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")
2 Answers

I have modified your code a little bit. I replaced the outcome list with a dictionary where the key is the sum and value is the frequency of the sum occurring. The output of the code is also pasted below. You can see below that the probabilities for the dice roll are close to what is expected mathematically.

Note: Python uses a pseudo random number generator for randint which is a very good approximation but not truely random

from random import randint
outcome = {2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0,10:0,11:0,12:0} #map of sum:freq
sim =100000

for simulations in range(sim):
    first_dice_roll = randint(1,6)
    second_dice_roll = randint(1,6)
    sum_dice = first_dice_roll + second_dice_roll
    outcome[sum_dice] += 1 
    
for key in outcome.keys():
    print("Percentage for rolling a sum of %s is: %s"%(key,outcome[key]/sim*100))

OUTPUT

Percentage for rolling a sum of 2 is: 2.775
Percentage for rolling a sum of 3 is: 5.48
Percentage for rolling a sum of 4 is: 8.179
Percentage for rolling a sum of 5 is: 11.029
Percentage for rolling a sum of 6 is: 13.831
Percentage for rolling a sum of 7 is: 16.997
Percentage for rolling a sum of 8 is: 13.846
Percentage for rolling a sum of 9 is: 11.16
Percentage for rolling a sum of 10 is: 8.334999999999999
Percentage for rolling a sum of 11 is: 5.5489999999999995
Percentage for rolling a sum of 12 is: 2.819

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.

Related