#. our project is no. 4 in the list .
4. Suppose that you roll four 6-sided dice. Estimate the probability that the die rolls are such that the four dice can be paired off so that one pair sums to 7 and the other sums to 9.
becasuse the condition is paired dies' sum should be 7 or 9 ,so the choices for 2 chooses from 4 will be 4*3/2 =6 , that means there will be six different appearances that satisfy the requests(+1). besides these 6 phases, others are all no useful(+0).
in order to make it more accurate, we did two ways as follows and we have similar solution which is arround 0.075 which should be correct answer.
solution 1 :
times=[] # int the trials that fit for the condition
import numpy as np
for e in range(0,1000000): # input the trials we want
res = np.random.randint(1, 7, size=1) # die 1 : the random number on 6 sides die roll once
res2 =np.random.randint(1, 7, size=1) # die 2 : the random number on 6 sides die roll once
res3= np.random.randint(1,7,size=1) # die 3 : the random number on 6 sides die roll once
res4=np.random.randint(1,7,size=1) # die 4 : the random number on 6 sides die roll once
if res +res2 == 7 and res3+res4 == 9: # one posibility if the sum of no1 and no2 could be 7 and others two is 9, then add 1
i=1
elif res+res3==7 and res2+res4==9: # one posibility if the sum of no1 and no3 could be 7 and others two is 9, then add 1
i=1
elif res+res4==7 and res2+res3==9: # one posibility if the sum of no1 and no4 could be 7 and others two is 9, then add 1
i=1
elif res2+res3==7 and res+res4==9: # one posibility if the sum of no2 and no3 could be 7 and others two is 9, then add 1
i=1
elif res2+res4==7 and res+res3==9: # one posibility if the sum of no2 and no4 could be 7 and others two is 9, then add 1
i=1
elif res3+res4==7 and res+res2==9: # one posibility if the sum of no3 and no4 could be 7 and others two is 9, then add 1
i=1
else: # different from these 6 choices are unfit for the condition, then add 0
i=0
times.append(i) # keep trying the trials untill 1000000 times
pro=np.mean(times) # def probability is mean of all "1"
print(pro)
the output : 0.074081
solution 2 :
def die_rolls(): # defining die_roll for multiple runs
D1= np.random.randint(1,7) # die 1 : random number on a six number die
D2=np.random.randint(1,7) # die 2 : random number on a six number die
D3=np.random.randint(1,7) # die 3 : random number on a six number die
D4=np.random.randint(1,7) # die 4 : random number on a six number die
if (D1+D2==7 and D3+D4==9) or (D1+D2 ==9 and D3+D4==7): # This is one of the possible result we want where
return 1 # D1 & D2 is a pair and D3 and D4 is a pair. and one pair
# adds up to 7 and the other adds up to 9. Which we return 1
elif (D2+D3==7 and D1+D4==9) or (D2+D3 ==9 and D1+D4==7):# This is one of the possible result we want where
return 1 # D2 & D3 is a pair and D1 and D4 is a pair. and one pair
# adds up to 7 and the other adds up to 9. Which we return 1
elif (D2+D4==7 and D1+D3==9) or (D2+D4 ==9 and D1+D3==7):# This is one of the possible result we want where
return 1 # D2 & D4 is a pair and D1 and D3 is a pair. and one pair
# adds up to 7 and the other adds up to 9. Which we return 1
else: # (Unwanted result) None of the statement above is true /
return 0 # Isn't a result where one pair adds up to 7 and the other
# pair adds up to 9. So it returns 0
sum([die_rolls() for i in range(1000000)])/1000000 # adds up all the '1's (wanted results) and divide it by the
# total number of trials. (in this case 1000000)
the output : 0.073664