有 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