Khan
Khan

Reputation: 33

Calculating the random sum and its probabilities for 1-6 dice

My error message is

File "/Users/CoconutTime/Downloads/sista_bomben.py", line 60, in probability_sums2 percentage_2 = (count[j] / freq)*100 IndexError: list index out of range

and I don't really understand how to change it or how it is wrong. It can calculate the probability for 1 dice just fine, it's when it calculates more than 1 dice where it's all wrong.

for 10 throws it shows that the total probability for the sum of 2 dices (2,3,4,5...12) is zero. This can't be the case


#Programmet ska beräkna den relativa frekvensen för de olika utfallen (tärningssummorna) för 2 till 6 tärningar
import random

#tom lista för x antal tärningar. När vi skriver rolled.append()
rolled1 = []
rolled2 = []
rolled3 = []
rolled4 = []
rolled5 = []
rolled6 = []

#Hur många gånger 1-6 tärningar kastats
kastat1 = 0
kastat2 = 0
kastat3 = 0
kastat4 = 0
kastat5 = 0
kastat6 = 0

#Hur många gånger vi kastar tärningarna
freq = 10

def kast1():
    # Den random summa av 1 tärning
    dice1 = random.randrange(1,7)
    return dice1
def kast2():
    # Den random summa av 2 tärningar
    dice2 = random.randrange(1,7)+ random.randrange(1,7)
    return dice2
def kast3():
    # Den random summa av 3 tärningar
    dice3 = random.randrange(1,7)+ random.randrange(1,7)+ random.randrange(1,7)
    return dice3
def kast4():
    # Den random summa av 4 tärningar
    dice4 = random.randrange(1,7)+ random.randrange(1,7)+ random.randrange(1,7)+ random.randrange(1,7)
    return dice4
def kast5():
    # Den random summa av 5 tärningar
    dice5 = random.randrange(1,7)+ random.randrange(1,7)+ random.randrange(1,7)+ random.randrange(1,7)+ random.randrange(1,7)
    return dice5
def kast6():
    # Den random summa av 6 tärningar
    dice6 = random.randrange(1,7)+ random.randrange(1,7)+ random.randrange(1,7)+ random.randrange(1,7)+ random.randrange(1,7)+ random.randrange(1,7)
    return dice6

def probability_sums():
    # För i mellan 1-6, räkna hur många gånger 1-6 inträffar / antal kast och * hundra för att få det i procent
    for i in range(0,6):
        percentage = (count[i] / freq)*100
        # Vi vill ha vårt procent i string (text)
        procent = str(percentage)
        print('Probability for 1 dice:', i + 1, ':', procent)
    for j in range(1,12):
    # För j mellan 2-12, räkna hur många gånger 2,3,4...12 inträffar / antal kast och * hundra för att få det i procent
        percentage_2 = (count[j] / freq)*100
        procent_2 = str(percentage_2)
        print('Probability for 2 dice:', j + 1, ':', procent_2)
    for k in range(2,18):
        percentage_3 = (count[k] / freq)*100
        procent_3 = str(percentage_3)
        print('Probability for 3 dice:', k + 1, ':', procent_3)
    for l in range(3,24):
        percentage_4 = (count[l] / freq)*100
        procent_4 = str(percentage_4)
        print('Probability for 4 dice:', l + 1, ':', procent_4)
    for m in range(4,30):
        percentage_5 = (count[m] / freq)*100
        procent_5 = str(percentage_5)
        print('Probability for 5 dice:', m + 1, ':', procent_5)
    for n in range(5,36):
        percentage_6 = (count[n] / freq)*100
        procent_6 = str(percentage_6)
        print('Probability for  6 dice:', n + 1, ':', procent_6)

def summa_dice2():
    global rolled2
    dice2 = random.randrange(1,7)+ random.randrange(1,7)
    for i in range(2,13):
        if dice2 == i:
            rolled2.append(dice2)
            return rolled2
def summa_dice3():
    global rolled3
    dice3 = random.randrange(1,7) + random.randrange(1,7) + random.randrange(1,7)
    for i in range(3,19):
        if dice3 == i:
            rolled3.append(dice3)
            return rolled3
def summa_dice4():
    global rolled4
    dice4 = random.randrange(1,7) + random.randrange(1,7) + random.randrange(1,7) + random.randrange(1,7)
    for j in range(4,25):
        if dice4 == j:
            rolled4.append(dice4)
            return rolled4
def summa_dice5():
    global rolled5
    dice5 = random.randrange(1,7) + random.randrange(1,7) + random.randrange(1,7) + random.randrange(1,7) + random.randrange(1,7)
    for k in range(5,31):
        if dice5 == k:
            rolled5.append(dice5)
            return rolled5
def summa_dice6():
    global rolled6
    dice6 = random.randrange(1,7) + random.randrange(1,7) + random.randrange(1,7) + random.randrange(1,7) + random.randrange(1,7) + random.randrange(1,7)
    for l in range(6,37):
        if dice6 == l:
            rolled6.append(dice6)
            return rolled6


for i in range(1,freq + 1):
    antal_kast1 = kast1()
    rolled1.append(antal_kast1)
    kastat1 += 1

    antal_kast2 = kast2()
    rolled1.append(antal_kast2)
    kastat2 += 1

    antal_kast3 = kast3()
    rolled1.append(antal_kast3)
    kastat3 += 1

    antal_kast4 = kast4()
    rolled1.append(antal_kast4)
    kastat4 += 1

    antal_kast5 = kast5()
    rolled1.append(antal_kast5)
    kastat5 += 1

    antal_kast6 = kast6()
    rolled1.append(antal_kast6)
    kastat6 += 1

#count räknar hur många gånger 1,2,3,4,5 och 6 uppkommer i listan. Om listan ser ut som list=[1,2,1,1,3] så skulle list.count(1) visa 3 eftersom det finns 3 ettor i listan.
count = [rolled1.count(1),rolled1.count(2),rolled1.count(3),rolled1.count(4),rolled1.count(5),rolled1.count(6)]
print('FOR 1 DICE:\nAfter being rolled',freq,' times:\n\n1 is rolled ',rolled1.count(1),'times\n2 is rolled ',rolled1.count(2),'times\n3 is rolled ',rolled1.count(3),'times\n4 is rolled ',rolled1.count(4),'times\n5 is rolled',rolled1.count(5),'times\n6 is rolled',rolled1.count(6),' times\n')

count2 = [rolled2.count(2),rolled2.count(3),rolled2.count(4),rolled2.count(5),rolled2.count(6),rolled2.count(7),rolled2.count(8),rolled2.count(9),rolled2.count(10),rolled2.count(11),rolled2.count(12)]
print('FOR 2 DICES:\nAfter being rolled',freq,' times:\n2 is rolled ',rolled2.count(2),'times\n3 is rolled ',rolled2.count(3),'times\n4 is rolled ',rolled2.count(4),'times\n5 is rolled',rolled2.count(5),'times\n6 is rolled',rolled2.count(6),'times\n7 is rolled',rolled2.count(7),'times\n8 is rolled',rolled2.count(8),'\n9 is rolled',rolled2.count(9),'times\n10 is rolled',rolled2.count(10),'\n11 is rolled',rolled2.count(11),'times\n12 is rolled',rolled2.count(12),' times\n')

count3 = [rolled3.count(3),rolled3.count(4),rolled3.count(5),rolled3.count(6),rolled3.count(7),rolled3.count(8),rolled3.count(9),rolled3.count(10),rolled3.count(11),rolled3.count(12),rolled3.count(13),rolled3.count(14),rolled3.count(15),rolled3.count(16),rolled3.count(17),rolled3.count(18)]
print('FOR 3 DICES:\nAfter being rolled',freq,' times:\n3 is rolled ',rolled3.count(3),'times\n4 is rolled ',rolled3.count(4),'times\n5 is rolled',rolled3.count(5),'times\n6 is rolled',rolled3.count(6),'times\n7 is rolled',rolled3.count(7),'times\n8 is rolled',rolled3.count(8),'times\n9 is rolled',rolled3.count(9),'times\n10 is rolled',rolled3.count(10),'times\n11 is rolled',rolled3.count(11),'times\n12 is rolled',rolled3.count(12),'times\n13 is rolled',rolled3.count(13),'times\n14 is rolled',rolled3.count(14),'times\n15 is rolled',rolled3.count(15),'times\n16 is rolled',rolled3.count(16),'times\n17 is rolled',rolled3.count(17),'times\n18 is rolled',rolled3.count(18),' times\n')

count4 = [rolled4.count(4),rolled4.count(5),rolled4.count(6),rolled4.count(7),rolled4.count(8),rolled4.count(9),rolled4.count(10),rolled4.count(11),rolled4.count(12),rolled4.count(13),rolled4.count(14),rolled4.count(15),rolled4.count(16),rolled4.count(17),rolled4.count(18),rolled4.count(19),rolled4.count(20),rolled4.count(21),rolled4.count(22),rolled4.count(23),rolled4.count(24)]
print('FOR 4 DICES:\nAfter being rolled',freq,' times\n4 is rolled ',rolled4.count(4),'times\n5 is rolled',rolled4.count(5),'times\n6 is rolled',rolled4.count(6),'times\n7 is rolled',rolled4.count(7),'times\n8 is rolled',rolled4.count(8),'times\n9 is rolled',rolled4.count(9),'times\n10 is rolled',rolled4.count(10),'times\n11 is rolled',rolled4.count(11),'times\n12 is rolled',rolled4.count(12),'times\n13 is rolled',rolled4.count(13),'times\n14 is rolled',rolled4.count(14),'times\n15 is rolled',rolled4.count(15),'times\n16 is rolled',rolled4.count(16),'times\n17 is rolled',rolled4.count(17),'times\n18 is rolled',rolled4.count(18),'times\n18 is rolled',rolled4.count(18),'times\n19 is rolled',rolled4.count(19),'times\n20 is rolled',rolled4.count(20),'times\n22 is rolled',rolled4.count(22),'times\n23 is rolled',rolled4.count(23),'times\n24 is rolled',rolled4.count(24),' times\n')

count5 = [rolled5.count(5),rolled5.count(6),rolled5.count(7),rolled5.count(8),rolled5.count(9),rolled5.count(10),rolled5.count(11),rolled5.count(12),rolled5.count(13),rolled5.count(14),rolled5.count(15),rolled5.count(16),rolled5.count(17),rolled5.count(18),rolled5.count(19),rolled5.count(20),rolled5.count(21),rolled5.count(22),rolled5.count(23),rolled5.count(24),rolled5.count(25),rolled5.count(26),rolled5.count(27),rolled5.count(28),rolled5.count(29),rolled5.count(30)]
print('FOR 5 DICES:\nAfter being rolled',freq,' times\n5 is rolled',rolled5.count(5),'times\n6 is rolled',rolled5.count(6),'times\n7 is rolled',rolled5.count(7),'times\n8 is rolled',rolled5.count(8),'times\n9 is rolled',rolled5.count(9),'times\n10 is rolled',rolled5.count(10),'times\n11 is rolled',rolled5.count(11),'times\n12 is rolled',rolled5.count(12),'times\n13 is rolled',rolled5.count(13),'times\n14 is rolled',rolled5.count(14),'times\n15 is rolled',rolled5.count(15),'times\n16 is rolled',rolled5.count(16),'times\n17 is rolled',rolled5.count(17),'times\n18 is rolled',rolled5.count(18),'times\n18 is rolled',rolled5.count(18),'times\n19 is rolled',rolled5.count(19),'times\n20 is rolled',rolled5.count(20),'times\n22 is rolled',rolled5.count(22),'times\n23 is rolled',rolled5.count(23),'times\n24 is rolled',rolled5.count(24),'times\n25 is rolled',rolled5.count(25),'times\n26 is rolled',rolled5.count(26),'times\n27 is rolled',rolled5.count(27),'times\n28 is rolled',rolled5.count(28),'times\n29 is rolled',rolled5.count(29),'times\n30 is rolled',rolled5.count(30),' times\n')

count6 = [rolled6.count(6),rolled6.count(7),rolled6.count(8),rolled6.count(9),rolled6.count(10),rolled6.count(11),rolled6.count(12),rolled6.count(13),rolled6.count(14),rolled6.count(15),rolled6.count(16),rolled6.count(17),rolled6.count(18),rolled6.count(19),rolled6.count(20),rolled6.count(21),rolled6.count(22),rolled6.count(23),rolled6.count(24),rolled6.count(25),rolled6.count(26),rolled6.count(27),rolled6.count(28),rolled6.count(29),rolled6.count(30)]
print('FOR 6 DICES:\nAfter being rolled',freq,'times\n6 is rolled',rolled6.count(6),'times\n7 is rolled',rolled6.count(7),'times\n8 is rolled',rolled6.count(8),'times\n9 is rolled',rolled6.count(9),'times\n10 is rolled',rolled6.count(10),'times\n11 is rolled',rolled6.count(11),'times\n12 is rolled',rolled6.count(12),'times\n13 is rolled',rolled6.count(13),'times\n14 is rolled',rolled6.count(14),'times\n15 is rolled',rolled6.count(15),'times\n16 is rolled',rolled6.count(16),'times\n17 is rolled',rolled6.count(17),'times\n18 is rolled',rolled6.count(18),'times\n18 is rolled',rolled6.count(18),'times\n19 is rolled',rolled6.count(19),'times\n20 is rolled',rolled6.count(20),'times\n22 is rolled',rolled6.count(22),'times\n23 is rolled',rolled6.count(23),'times\n24 is rolled',rolled6.count(24),'times\n25 is rolled',rolled6.count(25),'times\n26 is rolled',rolled6.count(26),'times\n27 is rolled',rolled6.count(27),'times\n28 is rolled',rolled6.count(28),'times\n29 is rolled',rolled6.count(29),'times\n30 is rolled',rolled6.count(30),'times\n31 is rolled',rolled6.count(31),'times\n32 is rolled',rolled6.count(32),'times\n33 is rolled',rolled6.count(33),'times\n34 is rolled',rolled6.count(34),'times\n35 is rolled',rolled6.count(35),'times\n36 is rolled',rolled6.count(36),' times\n')

probability_sums()
summa_dice2()
summa_dice3()
summa_dice4()
summa_dice5()
summa_dice6()

Upvotes: 0

Views: 298

Answers (3)

Chris Charley
Chris Charley

Reputation: 6613

Here is a partial solution that doesn't calculate the percentages, but could be extended to give that.

from random import randint
from collections import defaultdict
from pprint import pprint

def roll(n):
    score = 0
    for _ in range(n):
        score += randint(1,6)
    return score

freq = 100000

for rolls in range(1, 7):
    d = defaultdict(int)
    for _ in range(freq):
        d[roll(rolls)] += 1

    pprint(dict(d))

Prints:

{1: 16739, 2: 16472, 3: 16675, 4: 16711, 5: 16730, 6: 16673}
{2: 2702,
 3: 5679,
 4: 8185,
 5: 11069,
 6: 13769,
 7: 16838,
 8: 13987,
 9: 11229,
 10: 8213,
 11: 5537,
 12: 2792}
{3: 470,
 4: 1352,
 5: 2790,
 6: 4524,
 7: 7007,
 8: 9695,
 9: 11580,
 10: 12452,
 11: 12755,
 12: 11489,
 13: 9734,
 14: 6880,
 15: 4666,
 16: 2771,
 17: 1390,
 18: 445}

...

{6: 4,
 7: 13,
 8: 45,
 9: 128,
 10: 256,
 11: 525,
 12: 941,
 13: 1589,
 14: 2361,
 15: 3528,
 16: 4850,
 17: 6175,
 18: 7267,
 19: 8304,
 20: 9030,
 21: 9339,
 22: 9238,
 23: 8386,
 24: 7374,
 25: 6218,
 26: 4832,
 27: 3593,
 28: 2466,
 29: 1606,
 30: 929,
 31: 534,
 32: 280,
 33: 126,
 34: 45,
 35: 17,
 36: 1}

Upvotes: 1

Thierry Lathuille
Thierry Lathuille

Reputation: 24280

randrange(1, 6) will produce integers from 1 to 5 included, not 6. You should use randint(1, 6)

From the doc:

random.randint(a, b)

Return a random integer N such that a <= N <= b. Alias for randrange(a, b+1).

Upvotes: 1

Touten
Touten

Reputation: 306

Unless you have 5 face dices, you probably want to do randrange(1, 7).

Upvotes: 0

Related Questions