Get startedGet started for free

Can this loop run in parallel (2)?

You have enjoyed the previous dice game so much, you want to play it multiple times! You wrap the original code in a function:

play <- function() {
  total <- no_of_rolls <- 0
  while(total < 10) {
    total <- total + sample(1:6, 1)

    # If even. Reset to 0
    if(total %% 2 == 0) total <- 0 
    no_of_rolls <- no_of_rolls + 1
  }
  no_of_rolls
}

and construct a loop to play the game:

results <- numeric(100)
for(i in seq_along(results)) 
    results[i] <- play()

Do you think this for loop can be (easily) run in parallel?

This exercise is part of the course

Writing Efficient R Code

View Course

Hands-on interactive exercise

Turn theory into action with one of our interactive exercises

Start Exercise