% stagger.m
% A disoriented person starts at position 0, and takes N steps.
% Each step is randomly chosen to the left or to the right.
% Let D be the final distance of the person from the start.
%
% Compare D to sqrt(2*N/PI), which is the "expected value"
% that is, the average distance over many trials.
%
n = input ( '  Enter number of steps to take.' );

d = 0;
for k = 1 : n
  r = rand ( );
  if ( r < 0.5 )
    d = d - 1;
  else
    d = d + 1;
  end
end

fprintf ( '  After %d steps, distance = %g, sqrt(2*n/pi) = %g\n', ...
  n, abs ( d ), sqrt ( 2 * n / pi ) );
