I have codes in R, simulate Markov Chain
P = matrix(c(0,1,0,.2,0,.8,.1,.3,.6),nrow=3,byrow=T) n = 10000 x = numeric(n) pi0 = c(.5, 0, .5) state = sample(1:3, 1, p