Flipping Coins : Probability of Sequences vs Probability of Individuals Here is a problem I thought of:

*

*Suppose I am watching someone flip a fair coin. Each flip is
completely independent from the previous flip.

*I watch this person flip 3 consecutive heads.

*I interrupt this person and make the following offer: If the next flip results in a "head", I will buy you a slice of pizza; if the next flip results in a "tail", you will buy me a slice of pizza.

My Question: Who has the better odds of winning?

I wrote the following simulation using the R programming language. In this simulation, a "coin" is flipped many times ("1" = HEAD, "0" = TAILS). We then count the percentage of times HEAD-HEAD-HEAD-HEAD appears compared to HEAD-HEAD-HEAD-TAILS:
#load library
library(stringr)

#define number of flips
n <- 10^7

#flip the coin many times
set.seed(1)
flips = sample(c(0,1), replace=TRUE, size=n)

#count the percent of times HEAD-HEAD-HEAD-HEAD appears 
str_count(paste(flips, collapse=""), '1111') / n

0.0333663

#count the percent tof times HEAD-HEAD-HEAD-TAIL appears
str_count(paste(flips, collapse=""), '1110') / n

0.0624983

From the above analysis, it appears as if the person's luck runs out: after 3 HEADS, there is a 3.33% chance that the next flip will be a HEAD compared to a 6.25% chance the next flip will not be a HEAD (i.e. TAILS).
Thus, could we conclude: Even though the probability of each flip is independent from the previous flip, it becomes statistically more advantageous to observe a sequence of HEADS and then bet the next flip will be a TAILS? Thus, the longer the sequence of HEADS you observe, the stronger the probability becomes of the sequence "breaking"?

EDIT
The reason you are getting a different percentage for HHHH and HHHT is that you are calculating the instances of 1111 and 1110 in a very long string. you are not breaking these into blocks of 4. In a very long string it is more likely for you to have 3 ones in a row than it is to have 4 ones in a row. Since you aren't checking the groupings of 4 to make sure the flips are all in a single test, you will end up with more 1110 then you will 1111.
The correct way to code the problem is to group the coin flips into groups of 4. The following should be pretty easy to follow but is a bit slow.
#load library
library(stringr)

#define number of flips
n <- 100000

# Pre-assign a length of n to a data.frame
df <- data.frame(flip = character(n))

for(i in 1:n){
  df$flip[i] <- paste(sample(c(0,1),replace = TRUE,size = 4),collapse = "")
}

100*sum(df$flip == "1111")/n
# 6.259

100*sum(df$flip == "1110")/n
# 6.193

Original math based answer (missing code):
This is a common misinterpretation of statistics. Great example to learn from.
Your question was: After 3 coin flips, if I bet on the outcome of a 4th flip what is the probability of the 4th flip.
The 4th flip is now independent of the first 3 flips. There is no mechanism out there that grabs the coin and changes the probability of that 4th flip. The 4th flip will have a 50% chance of being heads, and a 50% chance of being tails.
Now, the question you are answering is: what is the probability a coin will be heads 4 times in a row. This is an entirely different question. The new question is asking what the probability is that you will get 4 heads in a row and this is a dependent question because not only does the 4th flip have to be heads, it depends on the first 3 having also been heads first. Then you have 16 possible combinations in 4 coin flips and only 1 possible way for it to come up with 4 heads (1/16 = 6.25%).
