Two people try to toss the same coin. The first person, say F starts tossing first, followed by the second person S. Who ever gets N number of heads first, will win. P represents the probability of getting Heads. To call the function 10000 times, and find the probability that the first person wins, use the following function call. In the following example function call, the first parm 0.2 represents the probability of getting HEAD, and 5 represents the required number of getting Heads for the person to win.

mean(replicate(10000,who_wins_first(0.2,5)))

who_wins_first <- function(P,N)
{
  #Two people try to toss the same coin.
  #The first person, say F starts tossing first, followed by the second person S. Who ever gets N number of heads first, will win
  #P represents the probability of getting Heads

  #To call the function 10000 times, and find the probability that the first person wins, use the following function call
  #mean(replicate(10000,who_wins_first(0.2,5)))
  
  #HF represents the outcome when first person tosses the coin first
  HF <- 0
  HS <- 0
  repeat
    {
       #0 represents tails, 1 represents H, in the following sample function
       F <- sample(0:1,1)
       if (F == 1)
       {
         HF <- (HF + 1)
         if (HF == N) 
           {
           break
           }
       
       }

    S <- sample(0:1,1)
    if (S == 1)
    {
      HS <- (HS + 1)
      if (HS == N) 
      {
        break
      }
      
    }
}
if(HS == HF) return(-1)
if(HS > HF) return(0)
if(HS < HF) return(1)
}
mean(replicate(10000,who_wins_first(0.2,5)))
## [1] 0.5473
mean(replicate(10000,who_wins_first(0.6,10)))
## [1] 0.5359
mean(replicate(10000,who_wins_first(0.2,15)))
## [1] 0.5262
mean(replicate(10000,who_wins_first(0.5,5)))
## [1] 0.5509
mean(replicate(10000,who_wins_first(0.5,10)))
## [1] 0.5338
mean(replicate(10000,who_wins_first(0.5,15)))
## [1] 0.5223
mean(replicate(10000,who_wins_first(0.5,20)))
## [1] 0.5242
mean(replicate(10000,who_wins_first(0.5,30)))
## [1] 0.5156
mean(replicate(10000,who_wins_first(0.5,40)))
## [1] 0.5173
mean(replicate(10000,who_wins_first(0.5,50)))
## [1] 0.5177
mean(replicate(10000,who_wins_first(0.5,60)))
## [1] 0.5105
mean(replicate(10000,who_wins_first(0.2,20)))
## [1] 0.5235
mean(replicate(10000,who_wins_first(0.5,70)))
## [1] 0.5116

To find the variance or standard deviation of the probability, use the following function call:

mean(replicate(10000,who_wins_first(0.5,5)))
## [1] 0.5498
sd(replicate(10000,who_wins_first(0.5,5)))
## [1] 0.4982913
var(replicate(10000,who_wins_first(0.5,5)))
## [1] 0.2471945