% Matlab program generating the Markov chain for the game of ladder % n=0; X(1) = 1; while X(n+1)<5; n=n+1; U=rand; X(n) = (U < .5)*1 + (U > .5)*( X(n-1)+1 ); end; n % number of steps it takes to reach the top % X % the history of X until the game is over % plot(X);