function z ( )

%*****************************************************************************80
%
%% z() uses a histogram to approximate the distribution of Z=X/(X-Y).
%
%  Discussion:
%
%    This code simulates the random variable Z=X/(X-Y),
%    where X and Y are independent and uniform from 0 to 1.
%    The output is a histogram of Z.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    29 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Dueling Idiots and Other Probability Puzzlers,
%    Princeton, 2012,
%    ISBN: 978-0691155005.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'z():\n' );
  fprintf ( 1, '  Simulate the random variable Z=X/(X-Y),\n' );
  fprintf ( 1, '  where X and Y are independent and uniform from 0 to 1.\n' );
  fprintf ( 1, '  The output is a histogram of Z.\n' );

  index = 10000;
  vector = zeros ( index, 1 );

  while ( 0 < index )
    X = rand ( );
    Y = rand ( );
    Z = X / ( X - Y );
    if ( abs ( Z ) < 3 )
      vector(index) = Z;
      index = index - 1;
    end
  end
%
%  Graphics.
%
  clf ( );
  x = -3.0 : 0.05 : 3.0;
  hist ( vector, x );
  title ( 'Histogram of Z=X/(X-Y)' );
  xlabel ( 'z, in bins of width 0.05' );
  ylabel ( 'bin population' );

  filename = 'z.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  return
end
