我有下面的代码,我被告知我需要使用不同的种子来生成我的随机数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');发布于 2013-10-31 15:22:03
抱歉弄得这么乱。我想你们中的一些人说我不需要产生不同的流是对的。
在上面的代码中,有一行写着“如果节点== pos",这是不正确的,应该是"if transmission_time(节点) == pos”。
这一行是导致我的图表不正常的原因。我还需要为成功的数据包生成新的随机数。
不过,谢谢你的建议!我知道了更多关于在Matlab中使用种子的随机性是如何工作的!
发布于 2013-10-29 17:03:37
来自matlab 文档
生成器的频繁重播不会改善输出的统计特性,也不会使输出在任何实际意义上更具有随机性。在重新启动MATLAB或运行涉及随机数的大型计算之前,重新播种可能非常有用。但是,在会话中频繁地重新播种生成器并不是一个好主意,因为随机数的统计属性可能会受到不利影响。
您不需要在rand中使用不同的种子:每次运行它时,它都不会生成相同的数字序列。例如
R = zeros(1e5,1);
for ii = 1:1e5
R(ii) = rand;
end
Rsorted = sort(R);
dRsorted = diff(Rsorted);
find(dRsorted == 0)将返回和空矩阵:rand从不返回相同的随机数在100,000个连续调用。
而且,在您的代码中,有一些错误。transmission_time = floor(i * rng(1, N));行应该读transmission_time = floor(i * rand(1, N));。
如果希望为每个周期使用不同的种子,可以在第一次使用rand之前添加以下调用:rng(i);。使用它,您将能够控制生成的随机数(rand将产生可预测的数字序列)。
发布于 2013-10-29 17:12:30
的确,如果您有某种模拟,用相同的随机数多次运行它是无用的。基本上有两种解决方案:
1.非常容易,不会产生可重复的结果
在代码开始时,将rng设置为基于now的内容。
这样你每次都会有不同的结果。
2.简单且推荐,生成可重复的结果
将您的模拟封装在一个循环中,如果您每次按顺序执行它们,您将得到与您的模拟结果不同的结果(从而允许您将结果平均),并且结果仍然可以被复制。
请注意,通常如果您想要减少模拟的波动率,您不需要多次运行它,而只需要让它运行更长时间。
https://stackoverflow.com/questions/19664162
复制相似问题