IniziaInizia gratis

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?

Questo esercizio fa parte del corso

Writing Efficient R Code

Visualizza il corso

Esercizio pratico interattivo

Passa dalla teoria alla pratica con uno dei nostri esercizi interattivi

Inizia esercizio