计算 1-6 个骰子的随机和及其概率



我的错误消息是

文件"/用户/椰子时间/下载/sista_bomben.py",第 60 行,probability_sums2 percentage_2 = (计数[j]/频率(*100 索引错误:列出索引超出范围

我真的不明白如何改变它或它是如何错误的。 它可以很好地计算 1 个骰子的概率,当它计算超过 1 个骰子时,一切都错了。

对于 10 次投掷,它表明 2 个骰子 (2,3,4,5...12( 的总概率为零。这不可能是这样


#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:nn1 is rolled ',rolled1.count(1),'timesn2 is rolled ',rolled1.count(2),'timesn3 is rolled ',rolled1.count(3),'timesn4 is rolled ',rolled1.count(4),'timesn5 is rolled',rolled1.count(5),'timesn6 is rolled',rolled1.count(6),' timesn')
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),'timesn3 is rolled ',rolled2.count(3),'timesn4 is rolled ',rolled2.count(4),'timesn5 is rolled',rolled2.count(5),'timesn6 is rolled',rolled2.count(6),'timesn7 is rolled',rolled2.count(7),'timesn8 is rolled',rolled2.count(8),'n9 is rolled',rolled2.count(9),'timesn10 is rolled',rolled2.count(10),'n11 is rolled',rolled2.count(11),'timesn12 is rolled',rolled2.count(12),' timesn')
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),'timesn4 is rolled ',rolled3.count(4),'timesn5 is rolled',rolled3.count(5),'timesn6 is rolled',rolled3.count(6),'timesn7 is rolled',rolled3.count(7),'timesn8 is rolled',rolled3.count(8),'timesn9 is rolled',rolled3.count(9),'timesn10 is rolled',rolled3.count(10),'timesn11 is rolled',rolled3.count(11),'timesn12 is rolled',rolled3.count(12),'timesn13 is rolled',rolled3.count(13),'timesn14 is rolled',rolled3.count(14),'timesn15 is rolled',rolled3.count(15),'timesn16 is rolled',rolled3.count(16),'timesn17 is rolled',rolled3.count(17),'timesn18 is rolled',rolled3.count(18),' timesn')
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,' timesn4 is rolled ',rolled4.count(4),'timesn5 is rolled',rolled4.count(5),'timesn6 is rolled',rolled4.count(6),'timesn7 is rolled',rolled4.count(7),'timesn8 is rolled',rolled4.count(8),'timesn9 is rolled',rolled4.count(9),'timesn10 is rolled',rolled4.count(10),'timesn11 is rolled',rolled4.count(11),'timesn12 is rolled',rolled4.count(12),'timesn13 is rolled',rolled4.count(13),'timesn14 is rolled',rolled4.count(14),'timesn15 is rolled',rolled4.count(15),'timesn16 is rolled',rolled4.count(16),'timesn17 is rolled',rolled4.count(17),'timesn18 is rolled',rolled4.count(18),'timesn18 is rolled',rolled4.count(18),'timesn19 is rolled',rolled4.count(19),'timesn20 is rolled',rolled4.count(20),'timesn22 is rolled',rolled4.count(22),'timesn23 is rolled',rolled4.count(23),'timesn24 is rolled',rolled4.count(24),' timesn')
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,' timesn5 is rolled',rolled5.count(5),'timesn6 is rolled',rolled5.count(6),'timesn7 is rolled',rolled5.count(7),'timesn8 is rolled',rolled5.count(8),'timesn9 is rolled',rolled5.count(9),'timesn10 is rolled',rolled5.count(10),'timesn11 is rolled',rolled5.count(11),'timesn12 is rolled',rolled5.count(12),'timesn13 is rolled',rolled5.count(13),'timesn14 is rolled',rolled5.count(14),'timesn15 is rolled',rolled5.count(15),'timesn16 is rolled',rolled5.count(16),'timesn17 is rolled',rolled5.count(17),'timesn18 is rolled',rolled5.count(18),'timesn18 is rolled',rolled5.count(18),'timesn19 is rolled',rolled5.count(19),'timesn20 is rolled',rolled5.count(20),'timesn22 is rolled',rolled5.count(22),'timesn23 is rolled',rolled5.count(23),'timesn24 is rolled',rolled5.count(24),'timesn25 is rolled',rolled5.count(25),'timesn26 is rolled',rolled5.count(26),'timesn27 is rolled',rolled5.count(27),'timesn28 is rolled',rolled5.count(28),'timesn29 is rolled',rolled5.count(29),'timesn30 is rolled',rolled5.count(30),' timesn')
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,'timesn6 is rolled',rolled6.count(6),'timesn7 is rolled',rolled6.count(7),'timesn8 is rolled',rolled6.count(8),'timesn9 is rolled',rolled6.count(9),'timesn10 is rolled',rolled6.count(10),'timesn11 is rolled',rolled6.count(11),'timesn12 is rolled',rolled6.count(12),'timesn13 is rolled',rolled6.count(13),'timesn14 is rolled',rolled6.count(14),'timesn15 is rolled',rolled6.count(15),'timesn16 is rolled',rolled6.count(16),'timesn17 is rolled',rolled6.count(17),'timesn18 is rolled',rolled6.count(18),'timesn18 is rolled',rolled6.count(18),'timesn19 is rolled',rolled6.count(19),'timesn20 is rolled',rolled6.count(20),'timesn22 is rolled',rolled6.count(22),'timesn23 is rolled',rolled6.count(23),'timesn24 is rolled',rolled6.count(24),'timesn25 is rolled',rolled6.count(25),'timesn26 is rolled',rolled6.count(26),'timesn27 is rolled',rolled6.count(27),'timesn28 is rolled',rolled6.count(28),'timesn29 is rolled',rolled6.count(29),'timesn30 is rolled',rolled6.count(30),'timesn31 is rolled',rolled6.count(31),'timesn32 is rolled',rolled6.count(32),'timesn33 is rolled',rolled6.count(33),'timesn34 is rolled',rolled6.count(34),'timesn35 is rolled',rolled6.count(35),'timesn36 is rolled',rolled6.count(36),' timesn')
probability_sums()
summa_dice2()
summa_dice3()
summa_dice4()
summa_dice5()
summa_dice6()

randrange(1, 6)将生成包含 1 到 5 的整数,而不是 6。你应该使用randint(1, 6)

从文档中:

random.randint(a, b(

返回一个随机整数 N,使得 a <= N <= b。 兰德朗日(A, B+1(.

这是一个部分解决方案,它不计算百分比,但可以扩展以给出百分比。

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

指纹:

{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}

除非你有 5 个面骰子,否则你可能想做 randrange(1, 7(。

最新更新