Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Random Integers Within Specfic Ranges
Date: Sat, 12 Mar 2011 03:47:06 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 20
Message-ID: <ileqbq$s0e$1@fred.mathworks.com>
References: <ildgd8$dhl$1@fred.mathworks.com> <ilelcd$b5f$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1299901626 28686 172.30.248.35 (12 Mar 2011 03:47:06 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 12 Mar 2011 03:47:06 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:715331

"David Dresden" <liquidnitrogenoverclocking@hushmail.com> wrote in message <ilelcd$b5f$1@fred.mathworks.com>...
> Well what happens if your first number you generate is like 17? You'd never be able to generate 5 numbers all less than 20 but > 17.
- - - - - - - - - - -
  David, if you were referring to my code, yes, I didn't adhere to the original request and simply used the range 1:N.  Below the code is modified to choose m rows of n random distinct integers in the range A:B.  Of course we must have n <= B-A+1.

x = zeros(m,n);
N = B-A+1;
p = zeros(1,N);
for j = 1:m
 p(1:N) = A:B;
 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

  Note: As I indicated earlier, this is very likely not optimum code.  I was experimenting with avoiding a sort operation and keeping the calls on 'rand' to a minimum.

Roger Stafford