Simulation output is often easier to inspect as a running total. cumsum shows how each run changes accumulated state.

Program

Play the script to add a per-run bonus and watch the final cumulative score change.

cumulative_runs.R
outcomes <- c(1, 0, 1, 1, 0, 1)
bonus <- 
scores <- outcomes + bonus
running_total <- cumsum(scores)
final <- tail(running_total, 1)
cat(final, "\n", sep = "")
outcomes <- c(1, 0, 1, 1, 0, 1)
bonus <- 
scores <- outcomes + bonus
running_total <- cumsum(scores)
final <- tail(running_total, 1)
cat(final, "\n", sep = "")
outcomes <- c(1, 0, 1, 1, 0, 1)
bonus <- 
scores <- outcomes + bonus
running_total <- cumsum(scores)
final <- tail(running_total, 1)
cat(final, "\n", sep = "")
cumsum `cumsum(scores)` returns the accumulated total after each run.
state A running total is a compact way to inspect simulation state over time.
tail `tail(running_total, 1)` selects the final simulated state.