min_time_next_event=time_next_event[i];
next_event_type=i;
}
/*Checktoseewhethertheeventlistisempty.*/
if(next_event_type==0){
/*Theeventlistisempty,sostopthesimulation.*/
fprintf(outfile,"\nEventlistemptyattime%f",sim_time);
exit
(1);
}
/*Theeventlistisnotempty,soadvancethesimulationclock.*/
sim_time=min_time_next_event;
}
voidarrive(void)/*Arrivaleventfunction.*/
{
floatdelay;
/*Schedulenextarrival.*/
time_next_event[1]=sim_time+expon(mean_interarrival);
/*Checktoseewhetherserverisbusy.*/
if(server_status==BUSY){
/*Serverisbusy,soincrementnumberofcustomersinqueue.*/
++num_in_q;
/*Checktoseewhetheranoverflowconditionexists.*/
if(num_in_q>Q_LIMIT){
/*Thequeuehasoverflowed,sostopthesimulation.*/
fprintf(outfile,"\nOverflowofthearraytime_arrivalat");
fprintf(outfile,"time%f",sim_time);
exit
(2);
}
/*Thereisstillroominthequeue,sostorethetimeofarrivalofthe
arrivingcustomeratthe(new)endoftime_arrival.*/
time_arrival[num_in_q]=sim_time;
}
else{
/*Serverisidle,soarrivingcustomerhasadelayofzero.(The
followingtwostatementsareforprogramclarityanddonotaffect
theresultsofthesimulation.)*/
delay=0.0;
total_of_delays+=delay;
/*Incrementthenumberofcustomersdelayed,andmakeserverbusy.*/
++num_custs_delayed;
server_status=BUSY;
/*Scheduleadeparture(servicecompletion).*/
time_next_event[2]=sim_time+expon(mean_service);
total_of_server+=time_next_event[2]-sim_time;
if((time_next_event[2]-sim_time)>19)
a19++;
}
}
voiddepart(void)/*Departureeventfunction.*/
{
inti;
floatdelay;
/*Checktoseewhetherthequeueisempty.*/
if(num_in_q==0){
/*Thequeueisemptysomaketheserveridleandeliminatethe
departure(servicecompletion)eventfromconsideration.*/
server_status=IDLE;
time_next_event[2]=1.0e+30;
}
else{
/*Thequeueisnonempty,sodecrementthenumberofcustomersin
queue.*/
--num_in_q;
/*Computethedelayofthecustomerwhoisbeginningserviceandupdate
thetotaldelayaccumulator.*/
delay=sim_time-time_arrival[1];
total_of_delays+=delay;
/*Incrementthenumberofcustomersdelayed,andscheduledeparture.*/
++num_custs_delayed;
time_next_event[2]=sim_time+expon(mean_service);
total_of_server+=time_next_event[2]-sim_time;
if((delay+time_next_event[2]-sim_time)>19)
a19++;
/*Moveeachcustomerinqueue(ifany)uponeplace.*/
for(i=1;i<=num_in_q;++i)
time_arrival[i]=time_arrival[i+1];
}
}
voidreport(void)/*Reportgeneratorfunction.*/
{
/*Computeandwriteestimatesofdesiredmeasuresofperformance.*/
fprintf(outfile,"\n\nAveragedelayinqueue%11.3fminutes\n\n",
total_of_delays/num_custs_delayed);
fprintf(outfile,"Averagenumberinqueue%10.3f\n\n",
area_num_in_q/sim_time);
fprintf(outfile,"Serverutilization%15.3f\n\n",
area_server_status/sim_time);
fprintf(outfile,"Timesimulationended%12.3fminutes",sim_time);
printf("统计量:
\n");
printf("①修理店空闲的概率:
%24.3f\n",(sim_time-area_server_status)/sim_time);
printf("②店内有三个顾客的概率:
%20.3f\n",area_3_in_q/sim_time);
printf("③店内至少有一个顾客的概率:
%16.3f\n",abv_1/sim_time);
printf("④在店内顾客的平均数:
%22.3f\n",area_num_in_h/sim_time);
printf("⑤顾客在店内的平均逗留时间:
%16.3f\n",(total_of_delays+total_of_server)/num_custs_delayed);
printf("⑥顾客必须在店内消耗19分钟以上概率:
%8.3f\n\n",a19/num_custs_delayed);
printf("仿真时间:
%12.3fminutes\n\n",sim_time);
}
voidupdate_time_avg_stats(void)/*Updateareaaccumulatorsfortime-average
statistics.*/
{
floattime_since_last_event;
/*Computetimesincelastevent,andupdatelast-event-timemarker.*/
time_since_last_event=sim_time-time_last_event;
time_last_event=sim_time;
/*Updateareaundernumber-in-queuefunction.*/
area_num_in_q+=num_in_q*time_since_last_event;
/*Updateareaunderserver-busyindicatorfunction.*/
area_server_status+=server_status*time_since_last_event;
if(server_status==BUSY)
if(num_in_q==2)
area_3_in_q+=time_since_last_event;
if(server_status==IDLE)
if(num_in_q==3)
area_3_in_q+=time_since_last_event;
if(server_status==BUSY)
abv_1+=time_since_last_event;
if(server_status==IDLE)
if(num_in_q>0)
abv_1+=time_since_last_event;
if(server_status==BUSY)
area_num_in_h+=(num_in_q+1)*time_since_last_event;
if(server_status==IDLE)
area_num_in_h+=num_in_q*time_since_last_event;
}
floatexpon(floatmean)/*Exponentialvariategenerationfunction.*/
{
/*Returnanexponentialrandomvariatewithmean"mean".*/
return-mean*log(lcgrand(29));
}
/*Primemodulusmultiplicativelinearcongruentialgenerator
Z[i]=(630360016*Z[i-1])(mod(pow(2,31)-1)),basedonMarseandRoberts'
portableFORTRANrandom-numbergeneratorUNIRAN.Multiple(100)streamsare
supported,withseedsspaced100,000apart.Throughout,inputargument
"stream"mustbeanintgivingthedesiredstreamnumber.Theheaderfile
lcgrand.hmustbeincludedinthecallingprogram(#include"lcgrand.h")
beforeusingthesefunctions.
Usage:
(Threefunctions)
1.ToobtainthenextU(0,1)randomnumberfromstream"stream,"execute
u=lcgrand(stream);
wherelcgrandisafloatfunction.Thefloatvariableuwillcontainthe
nextrandomnumber.
2.Tosettheseedforstream"stream"toadesiredvaluezset,execute
lcgrandst(zset,stream);
wherelcgrandstisavoidfunctionandzsetmustbealongsettothe
desiredseed,anumberbetween1and2147483646(inclusive).Default
seedsforall100streamsaregiveninthecode.
3.Togetthecurrent(mostrecentlyused)integerinthesequencebeing
generatedforstream"stream"intothelongvariablezget,execute
zget=lcgrandgt(stream);
wherelcgrandgtisalongfunction.*/
/*Definetheconstants.*/
#defineMODLUS2147483647
#defineMULT124112
#defineMULT226143
/*Setthedef