```Path: news.mathworks.com!not-for-mail
From: "Siva " <sivaathome@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Random sampling with constraints
Date: Sun, 15 Apr 2012 03:09:17 +0000 (UTC)
Organization: Roche Diagnostics
Lines: 31
Message-ID: <jmde4t\$hd4\$1@newscl01ah.mathworks.com>
NNTP-Posting-Host: www-03-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1334459357 17828 172.30.248.48 (15 Apr 2012 03:09:17 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 15 Apr 2012 03:09:17 +0000 (UTC)
Xref: news.mathworks.com comp.soft-sys.matlab:764505

Hi -

I have a random [ 1 x m] vector of unique integers P. I need to randomly generate a [ 1 x n] vector S that is a subset of P that satisfies the requirement that the difference between any two integers in S is greater then "d".

For example if I start with a [ 1 x 7] integer vector P= [ 23 2 25 7 15 13 8 16], and "d" is 8, then one possible random [ 1 x 3] integer vector that satisfies my "difference constraint" is [ 23 2 13]. But neither [7 25 2] nor [ 25 23 2] are valid since the "difference constraint" is not satisfied with either one of them.

Here is what I do now:

% Parent random integer vector
P= [ 23 2 25 7 15 13 8 16] ;
d= 8 ;

% Preliminaries
n= 3 ; % desired number of integers in vector S
Q= sort( P) ;
S= Q( 1) ;  % select the first integer
nelements= 1 ; % number of integers found so far

% Loop until the desired number of integers are identified
for i= 2:length(Q)
if ( Q(i)-S(end)) > d
S= [ S Q( i)] ; nelements= nelements+1 ;
if nelements >= n, break, end % break out when the desired number of
% elements is found
end
end

This code generates a solution. But I believe the random vector S is 'biased' because of the sort and subsequent selection of the first element in the sorted vector as the first entry of S. I would appreciate any comments and/or suggestions.

Thanks.
Siva
```