Here’s R code for generating outcomes from a transition matrix

Let’s say the transition matrix is the following

1 -> 1 = 0.10, 1 -> 2 = 0.30, 1 -> 3 = 0.60

2 -> 1 = 0.00, 2 -> 2 = 0.20, 2 -> 3 = 0.80

3 -> 1 = 0.90, 3 -> 2 = 0.10, 3 -> 3 = 0.00

Here’s how the matrix looks:

t.matrix <- matrix(c(.1, .3, .6, 0, .2, .8, .9, .1, 0), byrow = T, nrow = 3, ncol =3)

t.matrix
##      [,1] [,2] [,3]
## [1,]  0.1  0.3  0.6
## [2,]  0.0  0.2  0.8
## [3,]  0.9  0.1  0.0

In this game, 1 should mostly be followed by 3 (0.60 chance), 2 should never be followed by 1 (0.00 chance), and 3 should almost always be followed by 1 (0.90 chance).

Let’s say the game has 50 trials, we’ll start by creating an empty vector called locations with a length of 50:

locations <- rep(NA, 50)

First, we’ll determine a random outcome for the first trial

locations[1] <- sample(3, 1)

Next, we’ll loop through the rest of the locations vector and create new outcomes based on previous outcomes and the transition matrix:

for(i in 2:length(locations)) {
                               
  # Get previous outcome
    previous.outcome <- locations[i - 1]
    
  # Get outcome probabilites from t.matrix based on
  # previous outcome
  
    next.outcome.probabilities <- t.matrix[previous.outcome,]
  
  # Select an outcome based on next.outcome.probabilities
    
    next.outcome <- sample(3, size = 1, prob = next.outcome.probabilities)
    
  # Add next.outcome to locations

    locations[i] <- next.outcome
    
}

Here is the final result

locations
##  [1] 2 2 3 1 1 1 2 3 1 3 2 3 1 3 1 3 1 3 1 3 1 1 3 1 3 1 3 1 3 1 3 2 3 1 3
## [36] 1 3 1 1 2 3 1 3 1 3 1 2 3 2 3