我正在做一个概率问题。在我的代码中,当我输入红蓝弓箭手的概率时,代码运行得很好。
from IPython.core import history
rng = default_rng(42)
def trial(red,blue , red_accurcy = 1, blue_accurcy = 1, history = False ,debug = False):
if history:
red_history = [red]
blue_history = [blue]
if debug:
print(f"Start:\t red = {red:3d} blue = {blue:3d}")
while red and blue:
p = np.array([red,blue] , dtype = float)
p /= p.sum()
arrow = rng.choice(['red' , 'blue'] , p=p)
if arrow == 'red':
blue -= 1 if rng.uniform() < red_accurcy else 0
else:
red -= 1 if rng.uniform() < blue_accurcy else 0
if debug:
print(f"\t red = {red:3d} blue = {blue:3d} arrow = {arrow}")
if history:
red_history.append(red)
blue_history.append(blue)
if debug:
print(f"End:\t red = {red:3d} blue = {blue:3d}")
if history:
return red,blue, red_history, blue_history
else:
return red, blue ,red_accurcy ,blue_accurcy
trial(90,45,0.1,0.2)
输出量:
(60, 0, 0.1, 0.2)
现在,我试着为红色选择不同的概率,看看蓝色能否获胜:
red_ac = np.linspace(0,1,10)
battles = [trial(90,45,red_accurcy=red_ac,blue_accurcy= 1) for red_ac in range(10)]
battles
输出量:
[(0, 45, 0, 1),
(75, 0, 1, 1),
(82, 0, 2, 1),
(80, 0, 3, 1),
(78, 0, 4, 1),
(74, 0, 5, 1),
(69, 0, 6, 1),
(74, 0, 7, 1),
(81, 0, 8, 1),
(81, 0, 9, 1)]
我看不太对。你能告诉我哪里出错了吗?
1条答案
按热度按时间vfhzx4xs1#
这个答案适用于上面的代码。
检查蓝色是否获胜
和输出: