题目
设某仓库前有一卸货场,货车一般是夜间到达,白天卸货,每天只能卸货 2 车,若一天内到达数超过 2 车,那么就推迟到次日卸货。根据表 3 所示的数据,货车到 达数的概率分布(相对频率)平均为 1.5 车/天,求每天推迟卸货的平均车数。
解: 这是单服务台的排队系统,可验证到达车数不服从泊松分布,服务时间也不服从指数分布(这是定长服务时间)。随机模拟法首先要求事件能按历史的概率分布规律出现。模拟时产生的随机数与事件的对应关系如表4
解答
import randomprob=[0.23,0.3,0.3,0.1,0.05,0.02]def possess_prob(prob):temp=0new_prob=[]for i in prob:temp+=inew_prob.append(temp)print(new_prob)return new_probdef once(prob1,day):wait=0cur_wait=0for i in range(day):temp = random.random()index = 0for i in prob1:if temp < i:breakindex += 1cur_wait+=indexif cur_wait>=2:cur_wait-=2else:cur_wait=0if cur_wait>0:wait+=cur_waitreturn cur_waitfrom Draw import Drawdef run(times,day):print("----start----")prob1=possess_prob(prob)result=[]sum=0for i in range(times):temp=once(prob1,day)sum+=templens=len(result)+1result.append(sum/lens)Draw([result], ["test"], range(len(result)), "MonteCarlo Method")run(10000,100)

