How to repeat a random number generation and simulation for 1000 times
Show older comments
Hi all, I have a code for random number generation and simulation for queuing experiment. The code is as below
code:
time=6;
lambda=5;
mean_arr=1/lambda;
num_arr=time*lambda*5;
interval=exprnd(mean_arr,1,num_arr);
intv(1,:)=interval;
arr_time(1,:)=cumsum(intv(1,:));
num_vaild=sum(arr_time(1,:)<=time);
data(1,:)=arr_time(1:num_vaild);
mean_treat=1/4;
std_treat=1/6;
data(2,:)=normrnd(mean_treat,std_treat,1,num_vaild);
for j=1:num_vaild
if data(2,j)<0
data(2,j)=0;
end
end
data(3,1)=0;
data(4,1)=data(1,1)+data(2,1);
patient=[1];
for i=2:num_vaild
queue=sum(data(4,patient)>data(1,i));
if queue==0;
data(3,i)=0;
data(4,i)=data(1,i)+data(2,i);
patient=[patient,i];
else num_patient=length(patient);
data(3,i)=data(4,num_patient)-data(1,i);
data(4,i)=data(4,num_patient)+data(2,i);
patient=[patient,i];
end
end
doctor_leavingtime = data(4,num_vaild);
I need to repeat this entire script for 1000 times and record the doctor's leaving time in data(4,end) every time, and create a vector for them.
any idea about how to repeat?
Answers (1)
Guillaume
on 14 Oct 2015
doctor_leavingtime = zeros(1, 1000);
for repeat = 1:1000
%your code to repeat
%...
doctor_leavingtime(repeat) = data(4, num_valid);
end
Unless I misunderstood your question that's all you have to do: wrap your original code in a loop.
Categories
Find more on View and Analyze Simulation Results in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!