我正在尝试运行一个小型赌场轮盘赌投注结果模拟器。结果,我想了解赌场应该有多少银行,才能不因玩家的下注规模而破产。 请参见下面的代码:
from random import choices
bankroll = 500
bet = 50
lose = -50
population = [bet, lose]
weights = [0.4865, 0.5135]
game_count = 1
winning_game_count = []
bankrupt_game_count = []
for i in range(10000):
while bankroll != 0 and game_count < 5000:
result = int(choices(population, weights)[0])
bankroll -= result
game_count += 1
if game_count > 4999:
winning_game_count.append('won')
elif bankroll == 0:
bankrupt_game_count.append('lost')
print(winning_game_count)
print(bankrupt_game_count)
我只得到上一次迭代的结果,而我想得到所有游戏(5000个游戏)的总和以及bankroll
导致0的游戏数量。我输了。
答案 0 :(得分:2)
由于只运行1次迭代,因此只能得到最后一次迭代的结果。 您需要重置每次迭代之间控制循环条件的变量:
for i in range(10000):
bankroll=500
game_count = 1
while bankroll != 0 and game_count < 5000: