%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% matlab code to roll a pair of dice N times and find the sum 

N=720;
die1=randi([1,6],[N,1]);
die2=randi([1,6],[N,1]);
x = die1 + die2;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% matlab code to do N series of coin tosses
% recording for each one how many tosses necessary
% until get 3 heads in a row

N=500;               % number of experiments
x=zeros(N,1);        % answers will go in here
for i=1:N
   % toss first 3 coins
   z1=randi([0,1]);
   z2=randi([0,1]);
   z3=randi([0,1]);
   x(i)=3;
   % so long as one of the current coins is zero, toss again
   while z1*z2*z3==0
       z1=z2;
       z2=z3;
       z3=randi([0,1]);
       x(i)=x(i)+1;
   end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% matlab code to roll three dice N times
%             and find the sum, the lowest and the highest 

N=500;
die1=randi([1,6],[N,1]);
die2=randi([1,6],[N,1]);
die3=randi([1,6],[N,1]);
sum = die1 + die2 + die3 ;
high = max( [die1,die2,die3], [] , 2 );
low  = min( [die1,die2,die3], [] , 2 );

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The points P1,P2,P3 are (0,0), (50,100), (100,0)
% A series of N points is defined as follows:
%    The first point is P1. 
%    To move from one point to the next, choose
%        at random one of P1,P2,P3 and move
%        half of the way there

P1=[0 0];
P2=[50 100];
P3=[100 0];

N=1000;
p=zeros(N,2);                    % these will be the points
p(1,:)=P1;                       % start at the point P1 
z=randi([1,3],[N-1,1]);

for i=1:(N-1)
    if z(i)==1
        p(i+1,:)=1/2*(P1+p(i,:));
    elseif z(i)==2
        p(i+1,:)=1/2*(P2+p(i,:));
    elseif z(i)==3
        p(i+1,:)=1/2*(P3+p(i,:));
    end
end

% plot the points! 

plot(p(:,1),p(:,2),'.')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
