【问题标题】:How can I debug my probability Python program?如何调试我的概率 Python 程序?
【发布时间】:2022-01-05 08:44:22
【问题描述】:

问题是:

在 Chuck-a-Luck 游戏中,我们掷三个骰子,每个骰子有六个面,编号为 1 到 6。玩家在一个数字上下注欧元。如果这个数字没有出现,欧元就会丢失。如果该数字出现一次或多次,玩家将赢得与该数字出现次数一样多的欧元。

这款游戏的预期价值是多少?平均支出是多少?

我计算出的值为:

dice1 = 6
dice2 = 6
dice3 = 6

total_posibility = 216                   # the total possibilities that three dice can have 
not_match_our_number = 5*5*5 = 125       # the outcome will not much our number
dice_1_and_2_match = 1*1*5               
dice_1_and_3_match = 1*5*1
dice_2_and_3_match = 5*1*1
total_of_2_dice_match = 15              # total possibilities that two dice outcome will be our number

total_posibility-(1+15+125) = 75        # total possibilities that one dice outcome will be our number

pr_3_match = 1/total_posibility
pr_2_match = 15/total_posibility
pr_1_match = 75/total_posibility
pr_no_match = 125/total_posibility

expected_value = 3*pr_3_match+2*pr_2_match+1*pr_1_match+-1*pr_no_match

expected_value
= -0.07870370370370372

但我正在尝试实现上述问题:

def run_trials(n=1,seed=None, debug=True):
  if seed is not None: rnd.seed(seed)

  dice_1 = [1,2,3,4,5,6]
  dice_2 = [1,2,3,4,5,6]
  dice_3 = [1,2,3,4,5,6]

  all_possibility = 216
  all_die = 1/216
  two_die = 15/216
  one_die = 75/216
  no_die  = 125/216

  win3 = 0
  win2 = 0
  win1 = 0
  lose = 0



  for k in range(n):
    if debug: print(f"trials: {k}")
    
    a = rnd.choice(dice_1)

    pick_1 = rnd.choice(dice_1 , size = 1, replace = True)
    pick_2 = rnd.choice(dice_2 , size = 1, replace = True)
    pick_3 = rnd.choice(dice_3 , size = 1, replace = True)

    if a ==  pick_1 == pick_2  == pick_3 :
      win3 = 3
      
    if a == pick_1 and  pick_2 or a == pick_1 and pick_3 or a == pick_2 and pick_3:
      win2 = 2
  
    if a ==  pick_1 or a == pick_2 or a == pick_3:
      win1 = 1
    if  a != pick_1 or a!= pick_2 or a!= pick_3:
      lose = -1
    
  expected = win3*all_die+win2*two_die+win1*one_die+lose*no_die
  if debug: print(f" expected value: {expected}")

run_trials(seed = 60, debug = True)

运行:

trials: 0
 expected value: -0.5787037037037037

但是,我得到了错误的答案。我可以尝试解决什么问题?

【问题讨论】:

  • 也尝试打印出win3win2win1lose 的各个值。我认为他们不会像你期望的那样行事。
  • @aschepler 你是对的,我得到了 0,0,0 和 -1。这是因为= 还是我使用if 作为错误选项?谢谢

标签: python probability


【解决方案1】:

我已经更新了我的代码,你们的 cmets 将帮助我改进。

 def run_trials(n = 100, seed = None , debug=True):
  if seed is not None: rnd.seed(seed)
  num =rnd.choice([6])
  

  all_possibility = 216
  all_die = 1/216
  two_die = 15/216
  one_die = 75/216
  no_die  = 125/216

  success1 = 0
  success2 = 0
  success3 = 0
  lose = 0
  
  for k in range(n):
    d1 = rnd.choice([1,2,3,4,5,6],size = 1, replace=True)
    d2 = rnd.choice([1,2,3,4,5,6],size = 1, replace=True)
    d3 = rnd.choice([1,2,3,4,5,6],size = 1, replace=True)
    if num == d1 and d2 and d3:
      if debug:print(f"\tAll die matched")
      success1 = 3
    
    if num == d1 and d2 or d1 and d3 or d2 and d3:
      if debug:print(f"\tTwo die matched")
      success2 = 2
    
    if num == d1 or d2 or d3:
      if debug:print(f"\tOne die matched")
      success3 = 1
    
    if num != d1 or d2 or d3:
      if debug:print(f"\tNo die matched")
      lose =-1
    
     
    print(f"\t{success1}, {success2} , {success3} , {lose}")
    return success1*all_die+success2*two_die+success3*one_die+lose*no_die
  
run_trials(seed =1,debug = True)

如果运行 100000 次:

    compute_prob(1,6)
      n       pr
=========================
    Two die matched
    One die matched
    No die matched
    0, 2 , 1 , -1
     10        -0.0926
    Two die matched
    One die matched
    No die matched
    0, 2 , 1 , -1
    100        -0.0926
    Two die matched
    One die matched
    No die matched
    0, 2 , 1 , -1
   1000        -0.0926
    Two die matched
    One die matched
    No die matched
    0, 2 , 1 , -1
  10000        -0.0926
    Two die matched
    One die matched
    No die matched
    0, 2 , 1 , -1
 100000        -0.0926

对吗?

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 2011-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-31
相关资源
最近更新 更多