Clear Filters
Clear Filters

How do I generate a vector of random integers without repeats and with j+2 constraints?

1 view (last 30 days)
I need to generate a vector of 64 random integers with the following constraints:
  1. The number have to be between 1 and 9 (excluding 4);
  2. There can be no j+1 repeats (i.e. [5 6 6]);
  3. There have to be 16 j+2 repeats (i.e. [5 6 5]);
I can get constraints 1 & 2 to work alone as well as constraint 3 alone using while. However, when I include them together (see below), the script just continues and doesn't give an output.
clear all, clc
x = randi([1,8],64,1);
x(x==4) = 9; % converts all 4s into 9s
y = diff(x); y2 = [9; y]; x2 = [x y2]; % repeat identifier column
while any(diff(x)==0)
while size(find(([x; 999; 999]-[999; 999; x])==0),1) ~=16
x = randi([1,8],64,1);
x(x==4) = 9;
end
end
x
length(find(diff(x)==0)) % gives the number of j+1 repeats
Any suggestions on where to go from here would be excellent.
Just so you know where I'm going with this, once I've accomplished this, I would like to add two further constraints:
4. I would like to restrict the probability of each integer to .125 (i.e. each integer would be included 8 times);
5. There would be 8 j+2 repetitions involving each integer.

Answers (2)

Paulo Silva
Paulo Silva on 27 Jan 2011
%constraints 1 2 3
x=[];
while(numel(x)~=64) %get more values until we get enough
c=0;
while(c==0)
x = randi([1,8],80,1);
x(x==4) = 9; %substitute 4 by 9
x(diff(x)==0)=[] %remove repeated values
%find 16 values repeated with other in the middle
for a=1:numel(x)
for b=1:2:numel(x)-2
if (x(b)==x(b+2))
c=c+1;
end
if (c==16)
break
end
end
end
end
end
  1 Comment
Todd Flanagan
Todd Flanagan on 28 Jan 2011
Devin says, "Paulo, your script meets constraints #1 and #2 but it doesn't meet #3, it consistently yields fewer than 16 j+2 repeats."

Sign in to comment.


Matt Fig
Matt Fig on 28 Jan 2011
Some of your constraints would seem to be exclusive at first read. Here is an attempt at all 5:
N = [1 2 3 5 6 7 8 9];
A = zeros(4,16);
for ii = 1:4:13
A(:,ii:ii+3) = repmat(reshape(N(randperm(8)),2,4),2,1);
end
B = randperm(16);
Vec = zeros(1,64);
Vec(1:4) = A(:,B(1)); % This is our resultant vector.
cnt = 5;
for ii = B(2:16)
T = A(:,ii);
if T(1) == Vec(cnt-1)
T = T(4:-1:1);
end
Vec(cnt:cnt + 3) = T;
cnt = cnt + 4;
end
  1 Comment
Todd Flanagan
Todd Flanagan on 28 Jan 2011
Devin says, "Matt, your script is very helpful as well. You are correct that my constraints are exclusive. I made a mistake - constraint #5 should be that there should be 2 j+2 repetitions involving each integer, resulting in a total of 16, as in constraint #3.
At present, your script doesn't appear very random but I imagine this is probably because you were trying to meet (my mistaken) constraint #5. If it reduced down to 2 j+2 repetitions per integer, it may work out."

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!