Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
array with random not repeated numbers

Subject: array with random not repeated numbers

From: Patrick Diviacco

Date: 10 Nov, 2009 22:04:02

Message: 1 of 10

hi,

how can I generate an array with random numbers (given a min and max) ?
Important: they have to be not repeated.

thanks

Subject: array with random not repeated numbers

From: ImageAnalyst

Date: 10 Nov, 2009 23:37:29

Message: 2 of 10

On Nov 10, 5:04 pm, "Patrick Diviacco" <patrick.divia...@gmail.com>
wrote:
> hi,
>
> how can I generate an array with random numbers (given a min and max) ?
> Important: they have to be not repeated.
>
> thanks

-----------------------------------------------------------
Did you see the rand() function?
So how about something like this
randomArray = minValue + (maxValue-MinValue)*rand(rows, columns);

Subject: array with random not repeated numbers

From: jrenfree

Date: 10 Nov, 2009 23:46:51

Message: 3 of 10

On Nov 10, 3:37 pm, ImageAnalyst <imageanal...@mailinator.com> wrote:
> On Nov 10, 5:04 pm, "Patrick Diviacco" <patrick.divia...@gmail.com>
> wrote:
>
> > hi,
>
> > how can I generate an array with random numbers (given a min and max) ?
> > Important: they have to be not repeated.
>
> > thanks
>
> -----------------------------------------------------------
> Did you see the rand() function?
> So how about something like this
> randomArray = minValue + (maxValue-MinValue)*rand(rows, columns);

Does that ensure that there won't be any repeated values though?

Subject: array with random not repeated numbers

From: Patrick Diviacco

Date: 10 Nov, 2009 23:49:02

Message: 4 of 10

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <45a8dd1f-8806-4903-ac8f-6a25382539f0@j19g2000vbi.googlegroups.com>...
> On Nov 10, 5:04?pm, "Patrick Diviacco" <patrick.divia...@gmail.com>
> wrote:
> > hi,
> >
> > how can I generate an array with random numbers (given a min and max) ?
> > Important: they have to be not repeated.
> >
> > thanks
>
> -----------------------------------------------------------
> Did you see the rand() function?
> So how about something like this
> randomArray = minValue + (maxValue-MinValue)*rand(rows, columns);


Is this code avoiding duplicates ?

Subject: array with random not repeated numbers

From: dpb

Date: 11 Nov, 2009 00:04:48

Message: 5 of 10

Patrick Diviacco wrote:
> ImageAnalyst <imageanalyst@mailinator.com> wrote in message <45a8dd1f-8806-4903-ac8f-6a25382539f0@j19g2000vbi.googlegroups.com>...
>> On Nov 10, 5:04?pm, "Patrick Diviacco" <patrick.divia...@gmail.com>
>> wrote:
>>> hi,
>>>
>>> how can I generate an array with random numbers (given a min and max) ?
>>> Important: they have to be not repeated.
>>>
>>> thanks
>> -----------------------------------------------------------
>> Did you see the rand() function?
>> So how about something like this
>> randomArray = minValue + (maxValue-MinValue)*rand(rows, columns);
>
>
> Is this code avoiding duplicates ?

No, but odds are pretty good unless the number you're asking for is very
high or the the range very constricted.

You can, of course, run a rejection/regeneration phase after the initial
generation.

--

Subject: array with random not repeated numbers

From: Namo

Date: 5 Jun, 2011 14:13:05

Message: 6 of 10

"Patrick Diviacco" wrote in message <hdco0i$1kk$1@fred.mathworks.com>...
> hi,
>
> how can I generate an array with random numbers (given a min and max) ?
> Important: they have to be not repeated.
>
> thanks

Do you mean integer numbers
for example if the number is from min=1 to max= 9

we need to generate array of length n=5
min=1
max=9
A=min:max
x=zeros(1,n)
for k=1:n
     x(k) = randsrc(1,1,A)
     a= find(A==x(k))
     A(a)=[];
end

Subject: array with random not repeated numbers

From: Roger Stafford

Date: 5 Jun, 2011 14:52:04

Message: 7 of 10

"Namo " <namo1983@hotmail.com> wrote in message <isg2tg$fvr$1@newscl01ah.mathworks.com>...
> "Patrick Diviacco" wrote in message <hdco0i$1kk$1@fred.mathworks.com>...
> > hi,
> >
> > how can I generate an array with random numbers (given a min and max) ?
> > Important: they have to be not repeated.
> >
> > thanks
>
> Do you mean integer numbers
> for example if the number is from min=1 to max= 9
>
> we need to generate array of length n=5
> min=1
> max=9
> A=min:max
> x=zeros(1,n)
> for k=1:n
> x(k) = randsrc(1,1,A)
> a= find(A==x(k))
> A(a)=[];
> end
- - - - - - - - - -
  For n random non-repeating integers in [min:max] just use randperm:

 p = randperm(max-min+1);
 p = p(1:n)+min-1;

Roger Stafford

Subject: array with random not repeated numbers

From: Derek O'Connor

Date: 6 Jun, 2011 20:11:05

Message: 8 of 10

"Patrick Diviacco" wrote in message <hdco0i$1kk$1@fred.mathworks.com>...
> hi,
>
> how can I generate an array with random numbers (given a min and max) ?
> Important: they have to be not repeated.
>
> thanks


See this thread:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/304272#824982

Derek O'Connor

Subject: array with random not repeated numbers

From: Roger Stafford

Date: 7 Jun, 2011 01:16:04

Message: 9 of 10

"Derek O'Connor" wrote in message <isjc8p$j7j$1@newscl01ah.mathworks.com>...
> See this thread:
>
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/304272#824982
>
> Derek O'Connor
- - - - - - - - - - -
  Thanks for reminding us of that thread, Derek. I had completely forgotten about that interesting discussion.

  This is an alternate method for choosing n non-repeating random integers out of [min:max] that avoids 'randperm' and its sorting:

 p = min:max;
 m = length(p);
 r = zeros(1,n);
 for k = m:-1:m-n+1
  q = ceil(k*rand);
  r((m+1)-k) = p(q); % <-- n random integers go to 'r'
  p(q) = p(k);
 end

  This method might be preferable in cases where max-min is so large as to require excessive time be spent sorting in 'randperm'.

Roger Stafford

Subject: array with random not repeated numbers

From: Derek O'Connor

Date: 7 Jun, 2011 06:44:04

Message: 10 of 10

"Roger Stafford" wrote in message <isju4k$6ok$1@newscl01ah.mathworks.com>...
> "Derek O'Connor" wrote in message <isjc8p$j7j$1@newscl01ah.mathworks.com>...
> > See this thread:
> >
> > http://www.mathworks.com/matlabcentral/newsreader/view_thread/304272#824982
> >
> > Derek O'Connor
> - - - - - - - - - - -
> Thanks for reminding us of that thread, Derek. I had completely forgotten about that interesting discussion.
>
> This is an alternate method for choosing n non-repeating random integers out of [min:max] that avoids 'randperm' and its sorting:
>
> p = min:max;
> m = length(p);
> r = zeros(1,n);
> for k = m:-1:m-n+1
> q = ceil(k*rand);
> r((m+1)-k) = p(q); % <-- n random integers go to 'r'
> p(q) = p(k);
> end
>
> This method might be preferable in cases where max-min is so large as to require excessive time be spent sorting in 'randperm'.
>
> Roger Stafford

------------------------------------

Roger,

I think your method is a modification of the Fisher-Yates shuffle
algorithm as implemented by Durstenfeld, CACM 1964. It is nearly
always preferable to Matlab's randperm which is O(n log n) because
it must sort. If speed is critical then Jan Simon's mexed Shuffle could
used:

http://www.mathworks.com/matlabcentral/fileexchange/27076-shuffle

If the sample size n is very much less the range N = max-min+1,
then the following function is fast and uses only O(n) memory.

%--------------------------------------------------------------
function p = RandSampNR(min,max,n);
% -------------------------------------------------------------
% Generate a random sample of size n from the range of N integers
% min:max, without replacement, using a rejection loop.
% Time Complexity is at least O(n^2) and is suitable for small
% n << N = max-min+1 only. Note that this is a Las Vegas algorithm
% whose running time is random, because of the rejection while-loop.
% Space Complexity is O(n).
%
% Derek O'Connor, 12 Mar 2011. derekroconnor@eircom.net
%
% USE: p = RandSampNR(-2^29,2^29,10^3)
% -------------------------------------------------------------
p = zeros(1,n);
N = max-min+1;
p(1) = min + floor(rand*N);
for k = 2:n
      r = min + floor(rand*N);
      while member(r,p,k-1)
           r = min + floor(rand*N);
      end
      p(k) = r;
end

function answer = member(e,v,k)
answer = false;
for i = 1:k
    if v(i) == e
        answer = true;
        return
    end
end

%---------------------------------------------------------------

Example:

>> tic;r = RandSampNR(-2^29,2^29,10^3);toc
Elapsed time is 0.012196 seconds.
Memory: about 8 Kbytes

>> tic;r = RandSampNRStafford2(-2^29,2^29,10^3);toc
Elapsed time is 10.120162 seconds.
Memory: about 8 Gbytes

Dell Precision 690, Intel 2xQuad-Core E5345 @ 2.33GHz 16GB RAM,
Windows7 64-bit Professional. MATLAB Version 7.6.0.324 (R2008a)


Derek O'Connor

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us