### 7.4.25 Generating a random walks: randmarkov

Given the transition matrix M for a Markov chain and an initial
state i_{0}, the command randmarkov(M,i_{0},n) will
generate a random walk (given as a vector) starting at i_{0} and
taking n random steps, where each step is a transition with
probabilities given by M. For example, if you enter

randmarkov([[0,1/2,0,1/2],[0,1,0,0],[1/4,1/4,1/4,1/4],[0,0,1/2,1/2]],2,10)

you might get

[2,3,2,0,3,2,2,0,3,2,0]

Alternatively, given a vector v = [n_{1},…,n_{p}], the command
randmatrix(v,i_{0}) will create a stochastic matrix with
p recurrent loops (given by v) and i_{0} transient states. If you
enter

randmarkov([1,2],2)

you might get

[[1.0,0.0,0.0,0.0,0.0],
[0.0,0.289031975209,0.710968024791,0.0,0.0],
[0.0,0.46230383289,0.53769616711,0.0,0.0],
[0.259262238137,0.149948861946,0.143448150524,0.242132758802,0.205207990592],
[0.231568633749,0.145429586345,0.155664673778,0.282556511895,0.184780594232]]