The Wayback Machine - https://web.archive.org/web/20220601025203/https://www.geeksforgeeks.org/probability-of-hitting-the-target-nth-time-at-mth-throw/
Skip to content
Related Articles

Related Articles

Probability of hitting the target Nth time at Mth throw

Improve Article
Save Article
Like Article
  • Difficulty Level : Hard
  • Last Updated : 30 May, 2022

Given integers N, M and p, the task is to find the probability of hitting a target for the Nth time at the Mth throw where p is the probability of hitting the target.

Examples:

Input: N = 1, M = 2, p = 0.3
Output: 0.21
Explanation: The target can be hit for the first time in the second throw only when the first throw is a miss.
So, the probability is multiplication of probability of hitting the target in second throw and probability of missing the target in the first throw = 0.3 * 0.7 = 0.21
because probability of missing  = 1 – probability of hitting.

Input: N = 8, M = 17, p = 0.4,
Output: 0.07555569565040642

 

Approach: The idea to solve the problem is based on the binomial distribution of probability

For getting Nth hit in the Mth throw there must be N-1 hits in the M-1 thrwos earlier and Nth throw must be a hit.
Say p is the probability of hitting and q is the probability of missing. q = 1 – p.
So the probability of getting N-1 hits in M-1 throws is: 
X = M-1CN-1 (pN-1qM-1-(N-1)) = M-1CN-1 (pN-1qM-N)
Therefore, the probability of hitting for Nth time is p*X = M-1CN-1 (pNqM-N)

Follow the below steps to implement the above idea:

  • Firstly, get the probability of not hitting a target.
  • Get the value of X as shown above.
  • Multiply the value of ‘p’ with it to get the actual answer.

Below is the implementation of the above approach.

Python3




# Python code to implement the approach
  
# Function to find the probability
# of Nth hit at Mth throw
def probab(p, m, n):
    q = 1-p
    res = fact(m-1)/fact(n-1)/fact(m-n)*p**(n-1)*q**(m-n)*p
    return res
  
# Function to find the factorial of a number
def fact(f):
    fact = 1
    for i in range(1, f + 1):
        fact = i * fact
    return fact
  
  
# Driver code
if __name__ == '__main__':
    p = 0.3
    M = 2
    N = 1
    print(probab(p, M, N))
Output
0.21

Time Complexity: O(M)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!