CommencerCommencer gratuitement

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?

Cet exercice fait partie du cours

Writing Efficient R Code

Afficher le cours

Exercice interactif pratique

Passez de la théorie à la pratique avec l’un de nos exercices interactifs

Commencer l’exercice