我希望通过以下功能进行1000次互动,以了解您是否在此游戏中获胜或放松。
游戏的设计是这样的,你扔掉一对骰子,然后拿回钱或者放钱。假设我们从5个硬币开始。
投出 12 会产生 1.5 个硬币。
投出 11 会产生 1 个硬币。
投出 10 会产生 0.5 硬币。
投出 9,8或7 不会产生任何结果。
从您的硬币数量中投掷 6,5,4,3,2或1 扣除 0.5 硬币。
到目前为止,这是我的实现:
def luckCalc():
amount = 5
# if 12 then 1/36 chance
if random.randrange(1,7) == 6 and random.randrange(1,7) == 6:
amount = amount + 1.5
# if 11 then 2/36 chance
elif (random.randrange(1,7) == 5 and random.randrange(1,7) == 6) or (random.randrange(1,7) == 6 and random.randrange(1,7) == 5):
amount = amount + 1
# if 10 then 3/36 chance
elif (random.randrange(1,7) == 5 and random.randrange(1,7) == 5) or (random.randrange(1,7) == 4 and random.randrange(1,7) == 6) or (random.randrange(1,7) == 6 and random.randrange(1,7) == 4):
amount = amount + 0.5
# if 9,8,7
# 4/36 + 5/36 + 6/36 chance
# 1+6, 2+5, 3+4, 4+3, 5+2, 6+1 chance
# 2+6, 3+5, 4+4, 5+3, 6+2 chance
# 3+6, 4+5, 5+4, 6+3 chance
# then no change in amount
# if 6,5,4,3,2,1
# chances...
# then amount -0.5
return amount
# Iterate over the dice throwing simulator and calculate total
total = 0.0
for a in range(1000):
total = total + luckCalc()
print (total)
我在功能结束时停止编码,因为我认识到必须有一个更优雅的解决方案来实现这一目标。任何有趣的建议,我一直听到的蒙特卡洛是什么?
答案 0 :(得分:2)
每次拨打random.randrange(1,7)
时,都会生成新随机数。由于您正在测试单个“转弯”,因此请滚动两次:
def roll_die():
return random.randrange(1, 7)
total = roll_die() + roll_die()
看看总和是否在一个范围内:
def play_turn():
total = roll_die() + roll_die()
if total == 12:
return 1.5
elif total == 11:
return 1.0
elif total == 10:
return 0.5
elif total <= 6:
return -0.5
else: # total is 7, 8, or 9
return 0
以下是100,000轮的结果:
>>> from collections import Counter
>>> counts = Counter(play_turn() for i in xrange(100000))
>>> counts
Counter({-0.5: 41823, 0: 41545, 0.5: 8361, 1.0: 5521, 1.5: 2750})
>>> probabilities = {score: count / 100000.0 for score, count in counts.items()}
>>> probabilities
{-0.5: 0.41823, 0: 0.41545, 0.5: 0.08361, 1.0: 0.05521, 1.5: 0.0275}
答案 1 :(得分:2)
你实际上可以将你正在做的所有事情都滚动到一个函数中:
from random import randrange
def play_game(rolls=1000, amount=5, n=6):
"""Play game 'rolls' times, starting with 'amount' on 'n'-sided dice."""
for i in range(rolls):
roll = randrange(1, n+1) + randrange(1, n+1)
if roll == 12:
amount += 1.5
elif roll == 11:
amount += 1
elif roll == 10:
amount += 0.5
elif roll < 7:
amount -= 0.5
return amount
答案 2 :(得分:0)
我注意到代码中的一些内容。首先,对于6-1个案例,你实际上并没有从金额中减去0.5。其次,既然你没有传递每个循环的初始数量,那么你的总数就会增加5到6.5之间,这使得总数毫无意义。
更有效的总数是每次传递金额:
def luckCalc( amount ):
然后为你的循环:
total = 5.0
for a in range(1000):
total = luckCalc(total)
Blender的答案刚刚在我写这篇文章时发布,是简化主要功能的好方法。
答案 3 :(得分:0)
我个人喜欢将结果表设置为数组(或字典,但这更符合我的目的,因为每个结果都是少数可能的整数之一),每个骰子的索引都设置为值由此产生的变化。见下文。
import random
def luckCalc(coins=5):
diceroll = random.randint(1,6)+random.randint(1,6) #roll them bones
#here's that table I was talking about....
results_table = ['index 0 is blank',"you can't roll a one on two dice",-.5,-.5,-.5,-.5,-.5,0,0,0,.5,1,1.5]
coins += results_table[diceroll] #changes your coins value based on your roll (as an index of results_table)
if results_table[diceroll] > 0: #change the string if your result was + or -
result = "gained {}".format(results_table[diceroll])
else:
result = "lost {}".format(results_table[diceroll]*-1)
print("You {} coins, putting you at {}".format(result,coins)) #report back to the user
return coins #this is how you save your output
#CONSTANTS GO HERE -- YOU CAN CHANGE THESE TO CHANGE YOUR PROGRAM
STARTING_COINS = 5
HOW_MANY_ITERATIONS = 1000
#this way we don't modify a constant
coins = STARTING_COINS
#do it how many times?
for _ in range(HOW_MANY_ITERATIONS): #oh yeah that many times
coins = luckCalc(coins) #runs the function and saves the result back to coins
#report to the user your final result.
print("After {} rolls, your final total is {}".format(HOW_MANY_ITERATIONS,coins))