function onion ( trial_num )

%*****************************************************************************80
%
%% onion() randomly slices the unit interval.
%
%  Discussion:
%
%    This code 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.
%
%  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.
%
%  Input:
%
%    integer trial_num: the number of trials.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'onion():\n' );
  fprintf ( 1, '  Uniformly slice the unit interval, from left to right,\n' );
  fprintf ( 1, '  ten times, and form the sum of the widths of the slices.\n' );
  fprintf ( 1, '  Do this %d times and plot the distribution function of the sum.\n', trial_num );

  rsum = zeros(1,trial_num);

  for cutting = 1 : trial_num

    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 = 0.01 : 0.01 : 5.0;
  dist(1) = rsum(1);
  for index = 2 : 500
    dist(index) = dist(index-1) + rsum(index);
  end

  dist = dist / trial_num;

  clf ( );
  plot ( x, dist, 'linewidth', 3 );
  grid ( 'on' );
  xlabel ( 'thickness' );
  ylabel ( 'probability' );
  title ( 'Distribution of Total Thickness' );
  filename = 'onion.png';
  print ( '-dpng', filename )
  fprintf ( '  Graphics saved as "%s"\n', filename );

  return
end
