问题描述
下面的代码告诉我,我需要使用不同的种子来生成10次随机数,然后对它们进行平均以得到更平滑的图形.我没有使用Matlab的丰富经验,因此即使在阅读了文档之后,我也不了解这些种子的工作原理.
I have the code below that I was told I need to use different seeds to generate my random numbers 10 times and then average those in order to get a smoother graph. I don't have much experience in using Matlab so I don't know much about how these seeds work even after reading the documentation.
% 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');
推荐答案
对于所有混乱,我们深表歉意.我认为有些人说的对,因为我不需要生成不同的流.
Sorry for all the confusion. I think some of you were right by saying I didn't need to generate different streams.
在上面的代码中,有一行显示"if node == pos",这是不正确的,应为"if transmission_time(node)== pos".
In the code above there is a line that says "if node == pos", this is incorrect and should be "if transmission_time(node) == pos".
那一行正是导致我的图表完全崩溃的原因.我还需要为成功的数据包生成新的随机数.
That one line was what was causing my graph to be all out of whack. I also needed to generate new random numbers for successful data packets as well.
谢谢您的所有建议!现在,我对Matlab中的种子如何使用随机性有了更多的了解!
Thanks for all your suggestions though! I know a lot more about how randomness works with seeds in Matlab now though!
这篇关于在Matlab中,如何生成10个随机种子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!