이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
How to generate random number generator in a nested loop
조회 수: 2 (최근 30일)
이전 댓글 표시
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
Rik
2020년 6월 28일
Does there need to be a pattern in the randomness? Otherwise you can simply round the output of rand.
Mariam Ali
2020년 6월 28일
@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
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Audio I/O and Waveform Generation에 대해 자세히 알아보기
태그
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)