Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Random Integers Within Specfic Ranges
Date: Mon, 14 Mar 2011 16:38:05 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 20
Message-ID: <illg9d$rlc$1@fred.mathworks.com>
References: <ildgd8$dhl$1@fred.mathworks.com> <ile3gj$6qq$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-02-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1300120685 28332 172.30.248.47 (14 Mar 2011 16:38:05 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 14 Mar 2011 16:38:05 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 2717340
Xref: news.mathworks.com comp.soft-sys.matlab:715739

Roger Stafford said

> - - - - - - - - - -
>   The following will make fewer calls on 'rand' than 'randperm' and it does not do a sort (though it may be slower nevertheless.)  The n-length rows of the output array x will be given non-repeating integers selected randomly from 1:N.  We must therefore have n <= N.  This is repeated for each of the m rows of x.
> 
> x = zeros(m,n);
> p = zeros(1,N);
> for j = 1:m
>  p(1:N) = 1:N;
>  for k = N:-1:N-n+1
>   q = ceil(k*rand);
>   x(j,N-k+1) = p(q);
>   p(q:k-1) = p(q+1:k);
>  end
> end
> 
> Roger Stafford


Thank you Roger S. I have just tried your code out and its spot on. Thank you very much!