Here's a long way that can help illustrate:
set.seed(101)
total <- 0
trials <- 0
limit <- 10
while(total < limit) {
trials <- trials + 1
total <- total + rbinom(1, 1, 0.5)
cat("Number of trials: ", trials, "\t", "Total is: ", total, "\n")
}
#> Number of trials: 1 Total is: 0
#> Number of trials: 2 Total is: 0
#> Number of trials: 3 Total is: 1
#> Number of trials: 4 Total is: 2
#> Number of trials: 5 Total is: 2
#> Number of trials: 6 Total is: 2
#> Number of trials: 7 Total is: 3
#> Number of trials: 8 Total is: 3
#> Number of trials: 9 Total is: 4
#> Number of trials: 10 Total is: 5
#> Number of trials: 11 Total is: 6
#> Number of trials: 12 Total is: 7
#> Number of trials: 13 Total is: 8
#> Number of trials: 14 Total is: 9
#> Number of trials: 15 Total is: 9
#> Number of trials: 16 Total is: 10
However, calling rbinom
is going to be resource intensive. A different approach would be to set the n
argument of rbinom
to something large and than use something like:
large_trials <- rbinom(60, 1, 0.5)
large_trials
#> [1] 1 0 0 0 1 1 0 1 1 1 0 0 0 1 0 0 0 0 1 1 0 1 0 1 0 0 1 0 0 0 0 1 1 0 0
#> [36] 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 0 1 1 1 1 0
which.min(cumsum(large_trials) < limit)
#> [1] 22