# Is there a R function to find the maximize run within a given interval for a coin simulation?

hope you are doing well. So I was asked to design and conduct a simulation study to estimate the probability that the observed maximum run length (A run is a sequence of consecutive heads or tails) for a fair coin flipping experiment is in the interval [9, 11] in a sample size of n = 1000, this is my attempt so far

``````result <-replicate(10000,{ #replicate 10000 times

experiment <- sample(c("T","H"),size=1000,replace=TRUE) #1000 flips

expe_run <- rle(experiment) #find the run

expe_val <- expe_run\$values #values of run

expe_length <- expe_run\$lengths #length of run

as<-list(expe_length,expe_val) #make a list for sapply function

max_run <-sapply(as, FUN=max) #apply max function through out for both

head_run <-expe_length[which(expe_val=='H')] # show the head run

tail_run <-expe_length[which(expe_val=='T')] #show the tail run

max_run

})

probability <-table(result)/10000 #probability for run probability
``````

The problem is I don't know how to finish the question, which is estimates the probability that the observed maximum run length for a fair coin flipping experiment is in the interval [9, 11], even though I got the table for every possible probability. Can you please help me out? Thank you

A combination of the run length encoding function `rle` and using `dbinom` , `pbinom` , and `rbinom` will do this more simply.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.