function proportion_of_heads = cointosses(num_flips)
% cointosses returns the proportion of heads after num_flips tosses of a
% fair coin; we encode heads as 1 and tails as 0 and use a random integer
% generator to simulate the flips.

flips = zeros(1,num_flips);
% initialize the vector "flips" -- it starts off as a bunch of 0's


for i=1:num_flips
    flips(i) = randi(2)-1;
    % at each cell of the vector, generator a random integer between 1 and
    % 2 and subtract 1 from it (so we have a random integer between 0 and
    % 1)
end

% this for loop ends when we've put a head or tail (encoded as 1 or 0,
% respectively) into each cell of the vector flips


number_of_heads = sum(flips);
% since tails are encoded as 0, if we sum together all the entries in the
% vector flips, we add 1 each time we had a head. therefore number of heads
% is just the sum of the elements of flips.

proportion_of_heads = number_of_heads/num_flips;
% to get the proportion of heads, we divide the number of heads by the
% number of flips; this is the value that is returned by this function,
% since that's what we specified at the top

end