Writing a simple for loop
Let's write a for loop to repeat a process several times, incrementally storing the results in a vector. We will use the function that we created in the previous exercise to roll dice.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Probability Puzzles in R
अभ्यास निर्देश
- Indicate in the
forloop that the index should go from 1 to 10000. - Fill in the index for
outputto store each result. - Fill in the argument for
roll_diceto roll two dice.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Initialize a vector to store the output
output <- rep(NA, 10000)
# Loop for 10000 iterations
for(i in ___){
# Fill in the output vector with the result from rolling two dice
output[___] <- roll_dice(___)
}