function LOC = rand_point(X,Z)

% RAND_POINT samples a single random point from a given polygon

% FUNCTIONS CALLED: pt_in_poly.m

% INPUTS: X = boundary file in format of 'polyform.m'
%         Z = output of 'polyform.m'

% OUTPUTS: LOC = coordinates of the point
%          s   = new seed

            
   	% Define polygon
   
   	first = Z(1,1) ;
   	last = Z(1,2) ;
   
   	P = X(first:last,:) ; 
   
   	% Define containing rectangle
   
   	Mn = min(P) ; 
   	Mx = max(P) ;
   
   	x0 = Mn(1) ;
   	y0 = Mn(2) ;
   	x1 = Mx(1) ;
   	y1 = Mx(2) ;
   
   	% Generate point
   
   	kk = 0 ;
   
   	counter = 1;
   
   	while kk == 0 ;
      
         r = rand(1);
         
         x = x0 + r*(x1 - x0) ;
            
         r = rand(1);            
            
         y = y0 + r*(y1 - y0) ;
      
      	if pt_in_poly([x,y],P) == 1
         
         	LOC = [x,y] ;
         
         	kk = 1;
         
         end %end if
         
         % Check to avoid infinite cycles
         
         counter = counter + 1;
         
         if (kk == 0)&(counter == 100)
            
           	error('looping error') 
            
         end	         
         
      end %end point generation
      
   
      
         
         
