生成正/负1整数的伪随机序列

时间:2015-04-16 22:04:12

标签: matlab random sequence prng

有人可以帮我用Matlab创建一个长度为1000的简单伪随机序列+ -1整数吗?

即。一个序列,如

-1 -1 1 1 -1 -1 1 -1 -1 -1 1 1 1 1 -1 -1 -1 -1 1 

我尝试使用下面的代码,但这是RANGE -1到1,其中包含0个值。我只想要-1和1.谢谢

x = randi([-1 1],1000,1);

5 个答案:

答案 0 :(得分:5)

您可以尝试从[0,1]生成随机的浮点数序列,并将任何小于0.5的值设置为-1,将任何大于/的值设置为1:

x = rand(1000,1);
ind = x >= 0.5;
x(ind) = 1;
x(~ind) = -1;

我的另一个建议是使用sign功能与randn结合使用,以便我们可以生成正数和负数。 sign生成的值为-1,0,1,具体取决于输入的符号。如果输入为负,则输出为-1,正数时为+1,0时为0.您可以执行额外检查,将输出为0的任何值设置为-1或1:

x = sign(randn(1000,1));
x(x == 0) = 1;

另一个(受Luis Mendo启发)将使用[-1,1]的向量并使用randi生成1或2的序列,然后使用此样本并将样本放入此向量中: / p>

vec = [-1 1];
x = vec(randi(numel(vec), 1000, 1));

此代码可以扩展到vec可以是您想要的任何内容,我们可以从vec中的任何元素进行采样,以生成随机值序列(由Luis Mendo进行观察。谢谢!)

答案 1 :(得分:4)

一些替代方案:

x = 2*randi(2, 1000, 1)-3; %// generate 1 and 2 values, and transform to -1 and 1
x = 2*(rand(1, 1000, 1)<=.5)-1; %// similar to Rayryeng's answer but in one step
x = randsample([-1 1], 1000, true); %// sample with replacement from the set [-1 1]

答案 2 :(得分:0)

感谢您提供这些有用的答案。我认为这个主题可能足够普遍,它可能值得比较。

在我的设置中(Windows8.4 x64 i74820k cpu和R2014a),最快版本始终如一:

x=2*round(rand(L,1))-1;

比最慢的解决方案快半个数量级。希望这会有所帮助。

比较: figure comparing execution times for pseudo-random sign generation

代码:

L=[];
for expon=0:6
    for mant=1:9
    L=cat(1,L,mant*power(10,expon));
    end
end
clear expon mant

t1=zeros(length(L),1);
x=2*round(rand(L(1),1))-1;
for li=1:length(L)
    tic,
    x=2*round(rand(L(li),1))-1;
    t1(li)=toc;
end

t2=zeros(length(L),1);
x=(rand(L(1),1)>0.5)*2-1;
for li=1:length(L)
    tic,
    x=(rand(L(li),1)>0.5)*2-1;
    t2(li)=toc;
end

t3=zeros(length(L),1);
x=(randi([0,1],L(1),1)>0.5)*2-1;
for li=1:length(L)
    tic,
    x=(randi([0,1],L(li),1)>0.5)*2-1;
    t3(li)=toc;
end

t4=zeros(length(L),1);
x=rand(L(1),1);ind=x>=0.5;x(ind)=1;x(~ind)=-1;
for li=1:length(L)
    tic,
    x=rand(L(li),1);
    ind=x>=0.5;
    x(ind)=1;
    x(~ind)=-1;
    t4(li)=toc;
end

t5=zeros(length(L),1);
x=sign(randn(L(1),1));
for li=1:length(L)
    tic,
    x=sign(randn(L(li),1));
    x(x==0)=1;
    t5(li)=toc;
end

t6=zeros(length(L),1);
vec = [-1 1];
x=vec(randi(numel(vec),L(1),1));
for li=1:length(L)
    tic,
    x=vec(randi(numel(vec),L(li),1));
    t6(li)=toc;
end

t7=zeros(length(L),1);
x=2*randi(2,L(1),1)-3;
for li=1:length(L)
    tic,
    x=2*randi(2,L(li),1)-3;
    t7(li)=toc;
end

t8=zeros(length(L),1);
x=randsample([-1 1],L(1),true);
for li=1:length(L)
    tic,
    x=randsample([-1 1],L(li),true);
    t8(li)=toc;
end

clear x vec ind li

figure,
loglog(L,[t1 t2 t3 t4 t5 t6 t7 t8],'.-','linewidth',2)
grid on 
grid minor
title('Generating pseudo-random sequence +1/-1')
ylabel('Exec. Time [s]')
xlabel('Output Vector Length')
T{1}='x=2*round(rand(L(1),1))-1';
T{2}='x=(rand(L(1),1)>0.5)*2-1';
T{3}='x=(randi([0,1],L(1),1)>0.5)*2-1';
T{4}='x=rand(L(1),1);ind=x>=0.5;x(ind)=1;x(~ind)=-1';
T{5}='x=sign(randn(L(1),1))';
T{6}='vec=[-1 1];x=vec(randi(numel(vec),L(1),1))';
T{7}='x=2*randi(2,L(1),1)-3';
T{8}='x=randsample([-1 1],L(1),true)';
legend(T,'location','northwest')

答案 3 :(得分:0)

只需使用 randsrc 功能。

它将生成1和-1的随机序列。

例如

out = randsrc(2,3)

out =

-1    -1    -1
 1    -1     1

答案 4 :(得分:0)

x = rand(N,1);
y = sign(x-0.5);