Code covered by the BSD License  

Highlights from
Choosse randomly number of elements from a vector

Be the first to rate this file! 5 Downloads (last 30 days) File Size: 1.47 KB File ID: #32017
image thumbnail

Choosse randomly number of elements from a vector



30 Jun 2011 (Updated )

Choosse randomly a number of elements from a vector. Randomly change the position of the vector

| Watch this File

File Information

function [out]=random2(ini,fin,num)
% Choosse randomly a number (num) of elements from a vector [ini:fin]
% Randomly change the position of the vector. in pairs of twos
% Imput:
% ini: Beginning of the vector
% fin: End of the vector
% nun: Number of elements to choose from the range of vector [ini:fin]
% Output
% output vector
% Example
% [out]=random2(-20,20,10)
% out =
% -1 5 18 -3 1 -17 15 7 3 -6
% [out]=random2(5,50,10)
% out =
% 29 19 20 47 43 18 14 48 23 28

vr=ini:fin; % Reference Vector
lon=fin-ini; % variation or range of the vector

for nv=1:5*length(vr) % vector length.
    p1=ceil(lon*rand); % randomly chooses position to move.
    p2=ceil(lon*rand); % randomly selects the position where it is going to move.
    tp=vr(p1); % temporal position.
    vr(p1)=vr(p2); % Change inicial by end position.
    vr(p2)=tp; % Change end by inicial position.
   out=vr(1:num); % output vector

% contact:
% Dave Correa

Required Products Statistics Toolbox
MATLAB release MATLAB 7.10 (R2010a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (7)
14 Apr 2012 j_solar

Hey Jan, why do you say it is not useful due to the biased results?

I need a method like this but was wandering why it is not useful


01 Jul 2011 Dave Correa

The objective of the randm2 function is to generate N unique random numbers in the range [ini fin].

Now, I think it would be much more efficient to use the function randperm as proposed Jan.

Thanks for your input

01 Jul 2011 Matt Fig

Jan, you are absolutely correct. I made a mistake in understanding what this was supposed to do. Thanks for the catch.

Good thing I didn't provide a rating!

01 Jul 2011 Jan Simon

See also:

01 Jul 2011 Jan Simon

The results are not valid, e.g.: random2(0,1,2) always replies [0,1], the 3rd element of random2(0,2,3) is 2 always, random2(0,10,10) does never contain a 10. This is caused by "lon=fin-ini;", which must be updated to "fin-ini+1".
Running the loop 5 times is bewildering: According to D.E.Knuth it is enough to swap each element once with a random other one. Knuth's (aka Fisher-Yates) method can be found in the FEX already, see e.g.:,
I do not see a reason, why Matt's suggest cannot be written to a function. But the reply is not unique and it does not reply the 1st element (Matt: what's up?).
This would work: v=ini:fin; v=v(randperm(length(v))); out=v(1:num); It is faster than your method, but less efficient as the Fisher-Yates-Shuffle.
In the current state this function is not useful due to the biased results.

30 Jun 2011 Dave Correa

Dear Matt;

Thank you very much for your suggestion.

If, it is an option, but I needed to create a function as part of a system.

30 Jun 2011 Matt Fig

Why not use the simpler:

out = ini + ceil((fin-ini)*rand(1,num));

30 Jun 2011

update link

Contact us