LoslegenKostenlos loslegen

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?

Diese Übung ist Teil des Kurses

Writing Efficient R Code

Kurs anzeigen

Interaktive Übung

Setze die Theorie in einer unserer interaktiven Übungen in die Praxis um

Übung starten