第一次滚动时累积百分比停留在 0.00%
Cumulative Percentage stuck at 0.00% at the first roll
我写了一个代码,给出了 1,000,000 场掷骰子游戏的统计数据。我计算了赢得和输掉的总比赛的百分比,以及 总比赛中输赢的 累计百分比 直到并包括给定的掷骰数 (输出的第 3 列)
问题是它在第一卷时一直给我 0.00%
,为什么会这样?
我计算累计百分比的代码:
for i in range(0, len(rolls)):
p_resolved = rolls[i]*100/craps_game_number
c_resolved = 100*sum(rolls[0:i])/craps_game_number
if i!=len(rolls)-1:
print('{:>5}{:>19.2f}%{:>19.2f}%'.format(i+1,p_resolved,c_resolved))
else:
print('{:>5}{:>19.2f}%{:>19.2f}%'.format(i + 1, p_resolved, 100*sum(rolls)/craps_game_number))
如果需要,这里是完整的代码:
import random
def win_or_lose():
roll = random.randint(1,6)+random.randint(1,6)
if roll in [7,11]:
return 'Win',1 # return the outcome and attempts it took to decide
elif roll in [2,3,12]:
return 'Loss',1
else:
attempts=1
my_point=roll
while True:
roll=random.randint(1,6)+random.randint(1,6)
attempts+=1 # add 1 until the outcome is decided
if roll==my_point:
return 'Win',attempts # return the outcome and rounds
elif roll==7:
return 'Loss',attempts # return the outcome and rounds
else:
continue
rolls = [0]*25
wins=loss=0
craps_game_number=1000000
for _ in range(craps_game_number):
outcome,attempts = win_or_lose() # store the outcome and rounds it took to decide
if outcome=='Win':
wins+=1
else:
loss+=1
if attempts<=len(rolls):
rolls[attempts-1]+=1
else:
rolls[-1]+=1
print('Percentage of wins: {:.2f}%'.format(wins*100/(craps_game_number)))
print('Percentage of losses: {:.2f}%'.format(loss*100/(craps_game_number)))
print('Percentage of wins/losses based on total number of rolls')
print()
print('{:>25}{:>20} '.format('% Resolved','Cummulative %'))
print('{:<5}{:>20} {:>20}'.format('Rolls','on this roll','of games resolved'))
for i in range(0, len(rolls)):
p_resolved = rolls[i]*100/craps_game_number
c_resolved = 100*sum(rolls[0:i])/craps_game_number
if i!=len(rolls)-1:
print('{:>5}{:>19.2f}%{:>19.2f}%'.format(i+1,p_resolved,c_resolved))
else:
print('{:>5}{:>19.2f}%{:>19.2f}%'.format(i + 1, p_resolved, 100*sum(rolls)/craps_game_number))
如果您更改此行:
c_resolved = 100*sum(rolls[0:i])/craps_game_number
...为此:
c_resolved = 100*sum(rolls[0:i + 1])/craps_game_number
...你应该得到这样的输出,我认为这就是问题所要求的:
% Resolved Cummulative %
Rolls on this roll of games resolved
1 33.35% 33.35%
2 18.80% 52.15%
3 13.49% 65.64%
4 9.64% 75.28%
5 6.91% 82.19%
6 4.98% 87.16%
7 3.57% 90.73%
8 2.58% 93.31%
9 1.85% 95.16%
10 1.35% 96.51%
11 0.97% 97.48%
12 0.69% 98.17%
13 0.50% 98.67%
14 0.36% 99.03%
15 0.26% 99.29%
16 0.19% 99.48%
17 0.14% 99.62%
18 0.10% 99.72%
19 0.08% 99.80%
20 0.05% 99.85%
21 0.04% 99.89%
22 0.03% 99.92%
23 0.02% 99.94%
24 0.02% 99.96%
25 0.04% 100.00%
这将使累积结果的第一行成为 non-zero,并且还应更正最后一行之前所有后续行的值(通过 special-case 逻辑将其设置为 100% ).
我写了一个代码,给出了 1,000,000 场掷骰子游戏的统计数据。我计算了赢得和输掉的总比赛的百分比,以及 总比赛中输赢的 累计百分比 直到并包括给定的掷骰数 (输出的第 3 列)
问题是它在第一卷时一直给我 0.00%
,为什么会这样?
我计算累计百分比的代码:
for i in range(0, len(rolls)):
p_resolved = rolls[i]*100/craps_game_number
c_resolved = 100*sum(rolls[0:i])/craps_game_number
if i!=len(rolls)-1:
print('{:>5}{:>19.2f}%{:>19.2f}%'.format(i+1,p_resolved,c_resolved))
else:
print('{:>5}{:>19.2f}%{:>19.2f}%'.format(i + 1, p_resolved, 100*sum(rolls)/craps_game_number))
如果需要,这里是完整的代码:
import random
def win_or_lose():
roll = random.randint(1,6)+random.randint(1,6)
if roll in [7,11]:
return 'Win',1 # return the outcome and attempts it took to decide
elif roll in [2,3,12]:
return 'Loss',1
else:
attempts=1
my_point=roll
while True:
roll=random.randint(1,6)+random.randint(1,6)
attempts+=1 # add 1 until the outcome is decided
if roll==my_point:
return 'Win',attempts # return the outcome and rounds
elif roll==7:
return 'Loss',attempts # return the outcome and rounds
else:
continue
rolls = [0]*25
wins=loss=0
craps_game_number=1000000
for _ in range(craps_game_number):
outcome,attempts = win_or_lose() # store the outcome and rounds it took to decide
if outcome=='Win':
wins+=1
else:
loss+=1
if attempts<=len(rolls):
rolls[attempts-1]+=1
else:
rolls[-1]+=1
print('Percentage of wins: {:.2f}%'.format(wins*100/(craps_game_number)))
print('Percentage of losses: {:.2f}%'.format(loss*100/(craps_game_number)))
print('Percentage of wins/losses based on total number of rolls')
print()
print('{:>25}{:>20} '.format('% Resolved','Cummulative %'))
print('{:<5}{:>20} {:>20}'.format('Rolls','on this roll','of games resolved'))
for i in range(0, len(rolls)):
p_resolved = rolls[i]*100/craps_game_number
c_resolved = 100*sum(rolls[0:i])/craps_game_number
if i!=len(rolls)-1:
print('{:>5}{:>19.2f}%{:>19.2f}%'.format(i+1,p_resolved,c_resolved))
else:
print('{:>5}{:>19.2f}%{:>19.2f}%'.format(i + 1, p_resolved, 100*sum(rolls)/craps_game_number))
如果您更改此行:
c_resolved = 100*sum(rolls[0:i])/craps_game_number
...为此:
c_resolved = 100*sum(rolls[0:i + 1])/craps_game_number
...你应该得到这样的输出,我认为这就是问题所要求的:
% Resolved Cummulative %
Rolls on this roll of games resolved
1 33.35% 33.35%
2 18.80% 52.15%
3 13.49% 65.64%
4 9.64% 75.28%
5 6.91% 82.19%
6 4.98% 87.16%
7 3.57% 90.73%
8 2.58% 93.31%
9 1.85% 95.16%
10 1.35% 96.51%
11 0.97% 97.48%
12 0.69% 98.17%
13 0.50% 98.67%
14 0.36% 99.03%
15 0.26% 99.29%
16 0.19% 99.48%
17 0.14% 99.62%
18 0.10% 99.72%
19 0.08% 99.80%
20 0.05% 99.85%
21 0.04% 99.89%
22 0.03% 99.92%
23 0.02% 99.94%
24 0.02% 99.96%
25 0.04% 100.00%
这将使累积结果的第一行成为 non-zero,并且还应更正最后一行之前所有后续行的值(通过 special-case 逻辑将其设置为 100% ).