【问题标题】:Tracking a biased coin flip experiment - Binomial distribution in python跟踪有偏差的硬币翻转实验 - python 中的二项式分布
【发布时间】:2019-09-17 00:34:48
【问题描述】:

有 15% 的几率获得正面。 85% 的几率得到反面。我想看看我需要掷多少次硬币才能得到正面。

每次我掷硬币时,我都想把这个数字放在一个空列表中,说明我掷硬币所用的尝试次数。

一共要抛硬币100次

def coin_flips(n):
   for i in range(n): #for i in the number of coin flips
       #will continue until we break
       empty_list: []
       while True:
           #flip coin
           flipped_coins = np.random.choice(['tails', 'heads'], p = [0.85, 0.15])
          #add count number of flipped cons
           n += 1
           #if coin lands on heads
           if flipped_coins == 'heads':
               #add integer to empty list
               empty_list += n
       #if the coin lands on tails
       else:
           #flip the coin again until it lands on heads
               open_box = np.random.choice(['empty', 'elixir'], p = [0.85, 0.15])
               #add the count of coins flipped
               n += 1

       return empty_list
n = 10_000

【问题讨论】:

  • 你得到了什么结果?
  • 您实际上是在看负二项分布,我相信 - 第一次失败之前的成功次数,NB(1, 0.85)

标签: python probability probability-distribution


【解决方案1】:

如果你只是想要你掷硬币的次数直到你得到正面

def flip_coins(n, head_prob):
    flips = 0
    heads = False
    while not heads:
        flips += 1
        flipped_coins = np.random.choice(['tails','heads'], p=[1-head_prod, head_prob])
        heads = flipped_coins == 'heads'
    return flips

如果您想要一份所花费金额的清单:

total_flips = [flip_coins(100, .15) for n in range(10_000)]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    • 2010-10-03
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多