function onion ( )

%*****************************************************************************80
%
%% ONION randomly slices the unit interval.
%
%  Discussion:
%
%    This m-file uniformly slices the unit interval, from left to right,
%    ten times, and forms the sum of the successive widths of the slices. 
%    It does this 5000 times and then plots the distribution function of the sum.
%
%  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, 'ONION\n' );
  fprintf ( 1, '  This m-file uniformly slices the unit interval, from left to right,\n' );
  fprintf ( 1, '  ten times, and forms the sum of the widths of the slices. It does\n' );
  fprintf ( 1, '  this 5000 times and then plots the distribution function of the sum.\n' );

  rsum = zeros(1,5000);

  for cutting = 1 : 5000

    total_thickness = 0;
    L = 1;

    for dice = 1 : 10
      slice = L * rand ( );
      total_thickness = total_thickness + slice;
      L = slice;
    end

    index = round ( 100 * total_thickness + .5 );
    rsum(index) = rsum(index)+1;

  end
%
%  Graphics.
%
  x = .01 : .01 : 5;
  dist(1) = rsum(1);
  for index = 2 : 500
    dist(index) = dist(index-1) + rsum(index);
  end

  clf
  plot(x,dist/5000)
  grid
  xlabel('thickness')
  ylabel('probability')
  title('Fig.5.2-Distribution of Total Thickness from 5,000 Cuttings')
  print ( '-dpng', 'onion.png' )

  return
end
