Pages

Monday, June 23, 2014

Implementation of probability and solving problems in Python scripting language

Recently, I came across a probability related worksheet here expected to be solved by students of African Institute for Mathematical Sciences. The were expected to solve the problems using Random function of Python.
Here I would like to show you an attempt of solving those problems by a beginner.

Q. No. 1:

1. Suppose you have a fair six-sided die. Now suppose that you throw the die three times. What is the
probability that you will get:
(a) a 1 on the first throw, a 2 on the second throw, and a 3 on the third throw?
(b) a 1, a 2, and a 3 in any order?
(c) three sixes?

Answer :

(a)
Generally, the probability of obtaining 1 on first through is 1/6 as there are 6 possibilities.
Similarly, the possibility of obtaining 2 on second through and 3 on third through is also 1/3 each.
If P(A) =P(B) = P(C) = 1/6
Then, the total probability P =  P(A) * P(B) * P(C) = 1/6 ³
The python code is shown below :
success = 0
tries = 0
for first in range (0,6) :
    for second in range (0,6) :
        for third in range (0,6):
            if first == 1 and second == 2 and third == 3:
                success+=1
            tries+=1
print (float(success)/tries)


Answer = 0.004629629629629629 i.e. 1/6^3


(b)
a 1, a 2, and a 3 in any order?
Answer :
The number of possibilities of getting 1, 2 and 3 in any order are :
123
132
213
231
312
321
Hence, there are 6 possibilities.
But the total possibilities are 111 to 666. i. e. 6^3
Therefore, the total probability is 6/6^3 = 1/6^2
The python code for this problem is shown below :
success = 0
tries = 0
for first in range(0,6):
    for second in range(0,6):
        for third in range(0,6):
            if first == 1 :
                if second == 2 :
                    if third == 3:
                        success+=1
                elif second == 3:
                    if third == 2 :
                        success+=1
            elif first == 2 :
                if second == 1 :
                    if third == 3:
                        success+=1
                elif second == 3:
                    if third == 1 :
                        success+=1
            elif first == 3 :
                if second == 1 :
                    if third == 2:
                        success+=1
                elif second == 2:
                    if third == 1 :
                        success+=1                               
            tries+=1
print (float(success)/tries)

#Ans : 0.027777777777777776 i.e. 6/6^3 = 1/6^2

(c)
The probability of getting a six on first or second or third die is 1/6
The total probability is the sum of all these probabilities.
If P(A) =P(B) = P(C) = 1/6
Then, the total probability P = P(A) * P(B) * P(C) = 1/6 ³
The python code is shown below :
success = 0
tries = 0
for first in range(1,7):
    for second in range(1,7):
        for third in range(1,7):
            if first == 6 and second == 6 :
                if third == 6 :
                    success+=1
            tries+=1
print ('Success=',success)
print ('tries=',tries)
print ('Probability=',float(success)/tries)

#Ans = 0.004629629629629629 i.e. 1/6^3

CLICK HERE FOR THE ANSWER TO SECOND QUESTION

0 comments:

Post a Comment

Receive all updates via Facebook. Just Click the Like Button Below...