% stickExp
% A stick of unit length is randomly split in two pieces. Estimate the
% average length of the short piece.

n= 100000; % number of trials
total= 0; % accumulated length of short pieces so far

for k = 1:1:n
breakPt= rand();
shortPiece= min(breakPt, 1-breakPt);
total= total + shortPiece;
end

avgLength= total/n;
fprintf('Estimated average length is %.4f (%d trials)\n', ...
avgLength, n)