以下のコードは、異なるシードを使用して乱数を 10 回生成し、それらを平均してより滑らかなグラフを取得する必要があると言われています。私は Matlab の使用経験があまりないため、ドキュメントを読んでもこれらのシードがどのように機能するかについてはよくわかりません。
% Create an array
S = 0:20;
CW = 0:5:100;
S(1) = 0;
CW(1) = 0;
counter = 2; % Counter for the nuber of S
N = 20; % Number of nodes
% Collect data for each increment of 5 up to 100 for CW values
for i = 5:5:100
T = 10000 / i; % Total number of cycles
% Create array of next transmission times for N nodes
transmission_time = floor(i * rng(1, N));
total_success = 0;
% Loop for T cycles
for t = 1:T
% For 0 to the number of contention windows
for pos = 0:i-1
% Count the number of nodes that have the current CW
count = 0;
for node = 1:N
if transmission_time(node) == pos
count = count + 1;
end
end
% If there is more than 1, then a collision occurs
collision = false;
if count > 1
collision = true;
% If there is exactly 1, then there is a success
elseif count == 1
total_success = total_success + 1;
end
% If there is a collision, reassign new transmissions times
if collision == true
for node = 1:N
if node == pos
transmission_time(node) = floor(i * rand(1));
end
end
end
end
end
% Display the ratio of successes
S(counter) = total_success / (T * i);
counter = counter + 1;
end
% Plot the graph for Success vs CW
plot(CW, S, 'o-');
xlabel('Contention Window, CW');
ylabel('Throughput, S');