简体   繁体   中英

Generating a Pseudo-random sequence of plus/minus 1 integers

Can anybody help me create a simple pseudo-random sequence of +-1 integers with length 1000 using Matlab?

Ie a sequence such as

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

I tried using this code below but this is the RANGE -1 to 1, which includes 0 values. I only want -1 and 1. Thanks

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

You can try generating a random sequence of floating point numbers from [0,1] and any values less than 0.5 set to -1, and anything larger set to 1:

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

Another suggestion I have is to use the sign function combined with randn so that we can generate both positive and negative numbers. sign generates values that are either -1, 0, 1 depending on the sign of the input. If the input is negative, the output is -1, +1 when positive and 0 when 0. You could do an additional check where any values that are output to 0, set them to -1 or 1:

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

One more (inspired by Luis Mendo) would be to have a vector of [-1,1] and use randi to generate a sequence of either 1 or 2, then use this and sample into this vector:

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

This code can be extended where vec can be anything you want, and we can sample from any element in vec to produce a random sequence of values (observation made by Luis Mendo. Thanks!).

Some alternatives:

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]

Thanks for these many helpful answers. I figure this topic might be general enough it may well deserve a comparison.

In my setup ( Windows8.4 x64 i74820k cpu and with R2014a) the fastest version is consistently:

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

Being half an order of magnitude faster than the slowest solution. Hope this helps.

comparison: figure comparing execution times for pseudo-random sign generation

code:

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')

Simply user randsrc function.

It will generate random sequences of 1 and -1.

For example

out = randsrc(2,3)

out =

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM