So as an experiment i wanted to simulate an urn with 100 balls, and R should keep extracting one of them until i get a specific ball number (in the case below the ball n100), like:
urn <- c(1:100)
num <- 0
while(num != 100){
num <-sample(urn, 1, FALSE)
}
But what i wanted to see is how many trials it takes on average to extract the n100 after repeating this experiment 100.000 times. I'm new to R, so i tried to make a for loop for the basic experiment to repeat it 100.000 times. Here's the code:
rep <- 100000
urn <- c(1:100)
num <- 0
trials <- 0
tottrials <-0
for (i in 1:rep){
while(num != 100){
num <-sample(urn, 1, FALSE)
trials <- trials +1
}
tottrials <- tottrials + trials
}
cat(prettyNum(prove, big.mark="'"),tottrials/rep,"\n")
But with this method it only do the while loop 1 time and then it repeat the found number of trials 100.000 times (instead of re-find again a new number of trials. Proof: if i make it paste the number of trials it did, he will paste the same random one 100.000 times). Why? :(
num==100the first time, it never changes. I'm not sure where you think that's changing in you loop. It's running exactly as you've coded it.