How to generate random number generator in a nested loop

3 visualizzazioni (ultimi 30 giorni)
Hi! I’m new to matlab. I’m actually taking a course on cellular network and for an assignment I need to generate random 0 and 1 for data or voice connection in a nested loop.. it is a relatively large code and I want to get it checked by someone proficient in matlab.. please help
  4 Commenti
Rik
Rik il 28 Giu 2020
Does there need to be a pattern in the randomness? Otherwise you can simply round the output of rand.
Mariam Ali
Mariam Ali il 28 Giu 2020
@henry, here is the code
% Matlab program for M/M/1 queue.
clear;
clc;
QLIMIT=3; %100;
TOTCUS=30000;
CHANNEL=10; %total number of available channels
NEVNTS=2; % total number of events for data and voice
BUSY=1;
IDLE=0;
K=20;
mu=60; % service rate
for k=1:20
lambda_d=k*3; % arrival rate of data
lambda_v=k*2; % arrival rate of voice
y=randi([0,1]); % Generate 0 and 1 randomly (Voice=0 and Data=1)
IN=0; % total arrivals
INV=0; % total voice arrivals
IND=0; % total data arrivals
% Initialization
SA=23541; SS=90458; % rand seeds
TIME=0.0;
MARKEND=0;
SERVER=IDLE;
NIQ=0; % Queue occupancy
TLEVNT=0.0; % Time of previous event
NUMCUS=0; % Total customers
TOTDEL=0.0; % Total delay
DTOTDEL=0.0; % Total delay of data connections
ANIQ=0.0; % sum(Queue occupancy * time period)
AUTIL=0.0; % Server total utilization
BLOCK=0;
% TNE: time of next event
% TNE(1): time for next arrival
% TNE(2): time for next departure
% exponentially dist. inter-arrival time
rand('seed',SA);
TNE(1)=TIME+(-(1./lambda_v)*log(rand));
SUP=rand('seed');
TNE(2)=1.0e+30;
% Timing
while (MARKEND == 0)
MINTNE=1.0e+29;
NEXT=0; % index of next event
if(y==0) %voice arrival
for i=1:NEVNTS % if arrival (NEXT==1) and for departure (NEXT==2)
if(TNE(i) < MINTNE)
MINTNE=TNE(i);
NEXT=i;
end
end
if(NEXT == 0 )
MARKEND=1; % condition for ending simulation
end
TIME=MINTNE;
% Updating
TSLE=TIME-TLEVNT; % time elapsed since last event
TLEVNT=TIME;
ANIQ=ANIQ+NIQ*TSLE;
AUTIL=AUTIL+SERVER*TSLE;
if (NEXT==1)
% Arrival process for voice
IN=IN+1;
INV=INV+1;
% update next arrival time
rand('seed',SA);
TNE(1)=TIME+(-(1./lambda_v)*log(rand)); %interarrival time
SA=rand('seed');
if(SERVER == BUSY)
if(CHANNEL==0 && NIQ >= QLIMIT) % blocked
BLOCK=BLOCK+1;
elseif(CHANNEL==0 && NIQ<QLIMIT)
NIQ=NIQ+1; % queued
TARRVL(NIQ)=TIME; % record arrival time
else %customer moves directly to channel
CHANNEL=CHANNEL-1;
TARRVL(NIQ)=TIME; % record arrival time
end
else % arrival incase the server is idle (no connection in system)
NUMCUS=NUMCUS+1;
CHANNEL=CHANNEL-1;
SERVER=BUSY;
rand('seed',SS);
TNE(2)=TIME+(-(1./mu)*log(rand)); % schedule departure time
SS=rand('seed');
end
elseif(NEXT==2)
% Departure process
if(CHANNEL==10) %all 10 channels are available
SERVER=IDLE;
TNE(2)=1.0e+30;
elseif((0<CHANNEL)&&(CHANNEL<10)) % channel is partially occupied
SERVER=BUSY;
CHANNEL=CHANNEL+1;
DELAY=TIME-TARRVL(1);
TOTDEL=TOTDEL+DELAY;
NUMCUS=NUMCUS+1;
rand('seed',SS);
TNE(2)=TIME+(-(1./mu)*log(rand));
SS=rand('seed');
else
SERVER=BUSY;
NIQ=NIQ-1;
DELAY=TIME-TARRVL(1);
TOTDEL=TOTDEL+DELAY;
NUMCUS=NUMCUS+1;
rand('seed',SS);
TNE(2)=TIME+(-(1./mu)*log(rand));
SS=rand('seed');
% queued customers moved one position toward head of queue
for i=1:NIQ
TARRVL(i)=TARRVL(i+1);
end
end
end
% remove effect of initial conditions
if (NUMCUS==3000)
TIME1=TIME;
TOTDEL=0;
ANIQ=0;
AUTIL=0;
BLOCK=0;
NUMCUS1=NUMCUS;
IN=0;
elseif (NUMCUS > TOTCUS)
MARKEND=1;
end
elseif(y==1) %data arrival will use 2 channels, available channels=10
% exponentially dist. inter-arrival time
rand('seed',SA);
TNE(1)=TIME+(-(1./lambda_d)*log(rand));
SUP=rand('seed');
TNE(2)=1.0e+30;
% Timing
MINTNE=1.0e+29;
for i=1:NEVNTS
if(TNE(i) < MINTNE)
MINTNE=TNE(i);
NEXT=i;
end
end
if(NEXT == 0 )
MARKEND=1; % condition for ending simulation
end
TIME=MINTNE;
% Updating
TSLE=TIME-TLEVNT; % time elapsed since last event
TLEVNT=TIME;
ANIQ=ANIQ+NIQ*TSLE;
AUTIL=AUTIL+SERVER*TSLE;
if (NEXT==1)
% Arrival process for data
IN=IN+1;
IND=IND+1;
% update next arrival time
rand('seed',SA);
TNE(1)=TIME+(-(1./lambda_d)*log(rand));
SA=rand('seed');
if(SERVER == BUSY)
if(CHANNEL<2 && NIQ >= QLIMIT) % blocked
BLOCK=BLOCK+1;
elseif(CHANNEL<2 && NIQ<QLIMIT)
NIQ=NIQ+1; % queued
TARRVL(NIQ)=TIME; % record arrival time
else %customer moves to channel
CHANNEL=CHANNEL-2;
end
else % in service
NUMCUS=NUMCUS+1;
SERVER=BUSY;
rand('seed',SS);
TNE(2)=TIME+(-(1./mu)*log(rand)); % schedule departure time
SS=rand('seed');
end
elseif(NEXT==2)
% Departure process
if(CHANNEL==10) % all channels are available
SERVER=IDLE;
TNE(2)=1.0e+30;
elseif((1<CHANNEL) && (CHANNEL<9)) % channel is partially occupied
SERVER=BUSY;
CHANNEL=CHANNEL+2;
DELAY=TIME-TARRVL(1);
DTOTDEL=DTOTDEL+DELAY;
NUMCUS=NUMCUS+1;
rand('seed',SS);
TNE(2)=TIME+(-(1./mu)*log(rand));
SS=rand('seed');
else %customer in both queue and channel
SERVER=BUSY;
NIQ=NIQ-1;
DELAY=TIME-TARRVL(1);
DTOTDEL=DTOTDEL+DELAY;
NUMCUS=NUMCUS+1;
rand('seed',SS);
TNE(2)=TIME+(-(1./mu)*log(rand));
SS=rand('seed');
% queued customers moved one position toward head of queue
for i=1:NIQ
TARRVL(i)=TARRVL(i+1);
end
end
end
end
% remove effect of initial conditions
if (NUMCUS==300)
TIME1=TIME;
TOTDEL=0;
DTOTDEL=0;
ANIQ=0;
AUTIL=0;
BLOCK=0;
NUMCUS1=NUMCUS;
IN=0;
elseif (NUMCUS > TOTCUS)
MARKEND=1;
end
end % end of while(MARKEND==0)
%Report
AVGDEL=DTOTDEL/(NUMCUS-NUMCUS1); % average delay of data connections
AVGNIQ(k)=ANIQ/(TIME-TIME1); % average queue length
UTIL(k)=AUTIL/(TIME-TIME1); % server utilization
PBV=BLOCK/INV; % blocking rate of voice
PBD=BLOCK/IND; % blocking rate of data
rho_d(k)=lambda_d/mu;
rho_v(k)=lambda_v/mu;
Pb_d(k)=(1-rho_d(k)).*rho_d(k).^(QLIMIT+1)/(1-rho_d(k).^(QLIMIT+2)); % theoretical data blocking prob.
Pb_v(k)=(1-rho_v(k)).*rho_v(k).^(QLIMIT+1)/(1-rho_v(k).^(QLIMIT+2)); % theoretical voice blocking prob.
end %end of k
k=1:TOTCUS;
lambda_d=k*3;
lambda_v=k*2;
figure(1)
plot(lambda_v,Pb_v(k),'-')
hold;
xlabel('lambda_V');
ylabel('Voice Blocking probability');
figure(2)
plot(lambda_d,Pb_v(k),'--')
hold;
xlabel('lambda_D');
ylabel('Data Blocking probability');
figure(3)
plot(lambda_v,AVGDEL,'-')
hold;
xlabel('lambda_V');
ylabel('Mean Delay of Data Connection');
figure(4)
plot(lambda_d,AVGDEL,'--')
hold;
xlabel('lambda_D');
ylabel('Mean Delay of Data Connection');
It goes on for a long time and generates no result

Accedi per commentare.

Risposte (0)

Tag

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by