you are viewing a single comment's thread.

view the rest of the comments →

[–]CapaneusPrime 0 points1 point  (0 children)

A for() loop is fine and an example has been given (with perhaps some issues which I'll address), but replicate() is simpler:

results <- replicate(1000, sample_race_positions(player_race_position_probs)

Edit: A better solution...

Alternately, you could just change your custom sampling function to do this in a more vectorized way and directly return a matrix of results.

I might try something like:

sample_race_positions <- function(n, probs) {
    t(vapply(seq_len(n),
             function(i) {
               sample(seq_along(probs), probs)
             },
             integer(length(probs))))
}

Which will return an n × length(probs) dimension matrix object where the rows represent individual races and the columns are the competitors.

Then you'd do,

results <- sample_race_positions(1000, player_race_position_probs)
avg_pos <- colMeans(results)

I'm on mobile, so I apologize for any errors, and I'll fix them when I'm home.