這個迴圈可以平行執行嗎(2)?
你非常喜歡前一個擲骰子的遊戲,決定多玩幾次!你把原始程式碼包成一個函式:
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
}
並寫一個迴圈來玩遊戲:
results <- numeric(100)
for(i in seq_along(results))
results[i] <- play()
你覺得這個 for 迴圈可以(而且很容易)平行執行嗎?
本練習屬於課程
