mercredi 26 août 2020

Count until condition is true

Count how many time will make a sample of two numbers from a vector of 'x <- c(2,4,5,6,8,3)' with replacement before the two numbers add up to '11'. Stop loop as soon as it obtained two numbers exactly equal to 11 and then count how many samples was selected before the stop?

 x <- c(2,4,5,6,8,3)
 y <- c()
 repeat{y <- c(y, sample(x[!x %in% y], 1) )
if( sum(v2) == 11 )
break
 }

I am interested in how many count of sample of two that sum up to did not sum up to 11 before the first samples that did sum up to 11.

Aucun commentaire:

Enregistrer un commentaire