function [sample_space_out, distribution_out]= get_distribution_sum(sample_space_in,distribution_in,n)
%GET_DISTRIBUTION_SUM sums a discrete random variable n times.

%   SAMPLE_SPACE_IN=sample space of the input experiment

%   DISTRIBUTION_IN=distribution of sample_space_in. It must be specified
%       so that distribution_in(i) is the probability of the outcome
%       sample_space_in(i).

%Sums the experiment with itself once to get started. If n=2, this is the
%end of the program. See prob_convolve for more details on how the actual
%sum of the experiment with itself is calculated.
[sample_space2,distribution2]=...
    prob_convolve(sample_space_in,distribution_in,...
    sample_space_in,distribution_in);

%If n>2, we must sum the experiment which was output in the aboe line of
%code with the original experiment n-2 more times. That's all this loop
%does. There is an if check on n>2 just to avoid errors. Note that we just
%continually evolve the variables sample_space2 and distribution2 rather
%than carrying along a large variable. 
if n>2
    for i=3:n
        [sample_space2,distribution2]=...
            prob_convolve(sample_space_in,distribution_in,...
            sample_space2,distribution2);
    end
end

%outputs the final distribution and sample space.
distribution_out=distribution2;
sample_space_out=sample_space2;

end

