이 루프는 병렬로 실행할 수 있나요 (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 루프는 (쉽게) 병렬로 실행할 수 있다고 생각하시나요?
이 연습은 강의의 일부입니다
