在我的编程课中,我们正在进行一项任务,我们需要计算某个结果随机发生的频率。
情景如下......
人们在音乐会上排队,他们赠送3张免费门票。第一个人有50%的机会获得一张票。如果他得到一张票,那么这个过程会在第2个人身上重复。有50%的机会获得一张票,但是票数就少了一张。 我需要有一个程序来确定第六个人获得罚单的频率。这是我到目前为止所拥有的......
from __future__ import division
import random
trial =0
person =0
tickets_left =3
num6_w_ticket =0
for i in range (0,100):
while tickets_left != 0:
trial += 1
win =random.randint(1,2)
if win == 2:
tickets_left -= 1
person += 1
if win != 2:
person += 1
if win ==2 and trial ==6:
num6_w_ticket += 1
tickets_left -= 1
avg = (num6_w_ticket * 100) / (trial * 100)
print " >>>",avg,"% of the time, the 6th person got a ticket."
如果有人能发现问题,那就太好了!