function [A,B]=genrandtpcp(m,n,k,r,toler)
%        [A,B]=genrandtpcp(m,n,k,r,toler)
%% generate random problem A,B using constructed P rank r with k constraints
fprintf('generating random TPCP problem with  m,n,k,r given by %i %i %i %i  \n',m,n,k,r)
%i=sqrt(-1);   % better to use    1i
%% comment out next two lines to repeat the same 'random' problem
rng('shuffle')
saverng=rng;
rng(saverng)
  % generate Fi's using unitary Fi and prob distrib. p to get Ptr
	fprintf('generating cell F with unitaries for generating P \n')
	F=cell(r,1);
	p=rand(r,1)+1;
	p=p/sum(p);             % prob distrib.
	for rr=1:r,
		[temp,~]=qr(randn(m,n)+1i*randn(m,n));
		F{rr}=temp;
	end
        Ptr=FtoP(F,p,toler);
       %rankPs{1}=rank(Ptr);
% add I for last cell for unital constraint
k=k+1;
A=cell(k,1);   
B=cell(k,1);
for ii=1:k,
    if ii==k,
      A{k}=eye(n)/n;
      B{k}=eye(m)/m;
      fprintf('last constraint k= %i is the unital constraint \n',k);
    else
	[q,~]=qr(randn(n)+1i*randn(n));
	d=10*rand(n,1);
	%d=[1;zeros(n-1,1)];
	d=d/sum(d);
	A{ii}=q*diag(d)*q';
	A{ii}=(A{ii}+A{ii}')/2;
	[q,~]=qr(randn(m)+1i*randn(m));
	d=10*rand(m,1);
	d=d/sum(d);
	B{ii}=q*diag(d)*q';
	B{ii}=(B{ii}+B{ii}')/2;
	B{ii}=TP(m,n,Ptr,A{ii});  % use image to try and get feasible problem
     end
end

