Flip a coin. Success, you win 100, otherwise you lose 50. You will keep playing until you have money in your pocket a
. How can the value of a
at any iteration be stored?
a <- 100
while (a > 0) {
if (rbinom(1, 1, 0.5) == 1) {
a <- a + 100
} else {
a <- a - 50
}
}
As a final result, when the while
loop ends, I would like to be able to look at the value of a
for each iteration, instead of just the final result. I consulted the post on Counting the iteration in sapply, but I wasn't able to apply it to this case.