%   RandFastWords

%   This software is protected by german copyright and international treaties.             
%   Copyright 2004 Markus Junghöfer & Peter Peyk. All Rights Reserved.                     
%                                                                                          
%   THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE        
%   NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO,    
%   WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE    
%   OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS,
%   TRADEMARKS OR OTHER RIGHTS. COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT,       
%   INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE      
%   OR DOCUMENTATION.


global v
NotOk=1
StandOld=10000000
while NotOk
	v=[];
                                                                      
	v=zeros(1,25);
	v=[1];
	
	for i=2:25
		r=randperm(4);
		if v(i-1)==1
			a=[2:5];
		elseif v(i-1)==2
			a=[1 3 4 5];
		elseif v(i-1)==3
			a=[1 2 4 5];
		elseif v(i-1)==4
			a=[1 2 3 5];
		elseif v(i-1)==5
			a=[1 2 3 4];
		end
% 		if any(v(i-2:i-1)==1)
% 			a=[2:5];
% 		elseif any(v(i-2:i-1)==2)
% 			a=[1 3 4 5];
% 		elseif any(v(i-2:i-1)==3)
% 			a=[1 2 4 5];
% 		elseif any(v(i-2:i-1)==4)
% 			a=[1 2 3 5];
% 		elseif any(v(i-2:i-1)==5)
% 			a=[1 2 3 4];
% 		end
		c=a(r(1));
		v(i)=c;
	end
	
	v1=find(v==1); nv1=length(v1);
	v2=find(v==2); nv2=length(v2);
	v3=find(v==3); nv3=length(v3);
	v4=find(v==4); nv4=length(v4);
	v5=find(v==5); nv5=length(v5);
	
	if ~any([nv1 nv2 nv3 nv4 nv5]-5)
		w=char;
		w(v1)='a';
		w(v2)='b';
		w(v3)='c';
		w(v4)='d';
		w(v5)='e';
		w=w;
		
		ab=findstr(w,'ab'); nab=length(ab);
		ac=findstr(w,'ac'); nac=length(ac);
		ad=findstr(w,'ad'); nad=length(ad);
		ae=findstr(w,'ae'); nae=length(ae);
		
		ba=findstr(w,'ba'); nba=length(ba);
		bc=findstr(w,'bc'); nbc=length(bc);
		bd=findstr(w,'bd'); nbd=length(bd);
		be=findstr(w,'be'); nbe=length(be);
		
		ca=findstr(w,'ca'); nca=length(ca);
		cb=findstr(w,'cb'); ncb=length(cb);
		cd=findstr(w,'cd'); ncd=length(cd);
		ce=findstr(w,'ce'); nce=length(ce);
		
		da=findstr(w,'da'); nda=length(da);
		db=findstr(w,'db'); ndb=length(db);
		dc=findstr(w,'dc'); ndc=length(dc);
		de=findstr(w,'de'); nde=length(de);
		
		ea=findstr(w,'ea'); nea=length(ea);
		eb=findstr(w,'eb'); neb=length(eb);
		ec=findstr(w,'ec'); nec=length(ec);
		ed=findstr(w,'ed'); ned=length(ed);
		nVec=[nab nac nad nae nba nbc nbd nbe nca ncb ncd nce nda ndb ndc nde nea neb nec ned];
		Stand=std(nVec);
		% 	NN=5;
		% 	Diff= ...
		% 	abs(NN-nab)+abs(NN-nac)+abs(NN-nad)+abs(NN-nae)+ ...
		% 	abs(NN-nba)+abs(NN-nbc)+abs(NN-nbd)+abs(NN-nbe)+ ...
		% 	abs(NN-nca)+abs(NN-ncb)+abs(NN-ncd)+abs(NN-nce)+ ...
		% 	abs(NN-nda)+abs(NN-ndb)+abs(NN-ndc)+abs(NN-nde)+ ...
		% 	abs(NN-nea)+abs(NN-neb)+abs(NN-nec)+abs(NN-ned);
		
		if Stand<StandOld
			StandOld=Stand
			nVec=nVec
			v=v'
			pause
		end
	end
end
