-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunSingleLinkSim.m
35 lines (26 loc) · 1.32 KB
/
runSingleLinkSim.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
%% Function runSingleLinkSim()
% Parameters
% K - the number of packets in the application message
% p - the probability of failure
% N - the number of simulations to run
%
% Returns: the average numeric result across the total simulations
function result = runSingleLinkSim(K,p,N)
simResults = ones(1,N); % a place to store the result of each simulation
for i=1:N
txAttemptCount = 0; % transmission count
pktSuccessCount = 0; % number of packets that have made it across
while pktSuccessCount < K
r = rand; % generate random number to determine if packet is successful (r > p)
txAttemptCount = txAttemptCount + 1; % count 1st attempt
% while packet transmissions is not successful (r < p)
while r < p
r = rand; % transmit again, generate new success check value r
txAttemptCount = txAttemptCount + 1; % count additional attempt
end
pktSuccessCount = pktSuccessCount + 1; % increase success count after success (r > p)
end
simResults(i) = txAttemptCount; % record total number of attempted transmissions before entire application message (K successful packets) transmitted
end
result = mean(simResults);
end