python中二项分布有偏抛硬币实验的跟踪

2024-04-19 08:28:33 发布

您现在位置:Python中文网/ 问答频道 /正文

有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

Tags: oftheaddforifon硬币list
1条回答
网友
1楼 · 发布于 2024-04-19 08:28:33

如果你只想知道你掷硬币的次数

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)]

相关问题 更多 >