Consider a markov chain of 4 states $\{S_1, S_2, S_3, S_4\}$ described by the transition matrix
$$ A = \begin{bmatrix} .25 & .20 & .25 & .30 \\ .20 & .30 & .25 & .30 \\ .25 & .20 & .40 & .10 \\ .30 & .30 & .10 & .30 \end{bmatrix} $$
I understand that given an initial state $x^{(0)}$ the probability that the system is in state $i$ after $n$ steps is
$$ x^{(n)} = A^n x^{(0)} $$
However, I want to know how to generate a realization of a sequence states. For example, from the matrix above one possible sequence of states over $n = 0, 1, 2, 3$ steps could be
$$ S_1 \rightarrow S_3 \rightarrow S_4 \rightarrow S_1 $$
Is there a way to programmatically generate a particular sequence or realization from the transition matrix, either based on a random initial state or given an initial state $x^{(0)}$?