MATLAB: To generate a Pseudorandom sequence based on some constraints

conditional probabilityconstraintspseudorandomrandomizationtransitional probability

I have 4 base triplets- ABC, DEF, GHI, JKL. Also there are catch word- Z. I want a pseudorandomixed sequence generated such that: * a) Each triplet should be repeated 40 times * b)No repeated triplets should be present (e.g. …T1T1…) * c)No repeated pairs of triplets should be present (e.g. …T1T2T1T2…). * d)The catch should also be presented 40 times
Also can the code give me the transitional probability of first letter in each triplet? Eg. if a part of the random sequence is- ABC and DEF, what is the transitional (conditional) probability of D|C?
Can anyone help me do this?

Best Answer

Here's one possibility. The transition probabilities are always 1/5, except for every 5th transition, which are 1/4
function out = myfunc(N)
if nargin<1, N=40; end
A=zeros(5,N);
A(:,1)=randperm(5).';
for ii=2:N
A(1,ii)=randi(5);
z=(1:5).';
z(A(1,ii))=[];
A(2:end,ii) = z(randperm(4));
end
triplets={'ABC','DEF','GHI','JKL','Z'};
out=reshape(triplets(A),1,[]);