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$>
References: <ildgd8$dhl$> <ilelcd$b5f$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1299901626 28686 (12 Mar 2011 03:47:06 GMT)
NNTP-Posting-Date: Sat, 12 Mar 2011 03:47:06 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: comp.soft-sys.matlab:715331

"David Dresden" <> wrote in message <ilelcd$b5f$>...
> 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);

  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