Moved markov chain gen into a function.