Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
| Download
Image: ubuntu2004
#. our project is no. 4 in the list .14. 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.2becasuse 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).3in 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.45solution 1 :67times=[] # int the trials that fit for the condition8import numpy as np9for e in range(0,1000000): # input the trials we want10res = np.random.randint(1, 7, size=1) # die 1 : the random number on 6 sides die roll once11res2 =np.random.randint(1, 7, size=1) # die 2 : the random number on 6 sides die roll once12res3= np.random.randint(1,7,size=1) # die 3 : the random number on 6 sides die roll once13res4=np.random.randint(1,7,size=1) # die 4 : the random number on 6 sides die roll once14if 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 115i=116elif 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 117i=118elif 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 119i=120elif 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 121i=122elif 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 123i=124elif 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 125i=126else: # different from these 6 choices are unfit for the condition, then add 027i=028times.append(i) # keep trying the trials untill 1000000 times29pro=np.mean(times) # def probability is mean of all "1"30print(pro)3132the output : 0.074081333435solution 2 :36def die_rolls(): # defining die_roll for multiple runs37D1= np.random.randint(1,7) # die 1 : random number on a six number die38D2=np.random.randint(1,7) # die 2 : random number on a six number die39D3=np.random.randint(1,7) # die 3 : random number on a six number die40D4=np.random.randint(1,7) # die 4 : random number on a six number die41if (D1+D2==7 and D3+D4==9) or (D1+D2 ==9 and D3+D4==7): # This is one of the possible result we want where42return 1 # D1 & D2 is a pair and D3 and D4 is a pair. and one pair43# adds up to 7 and the other adds up to 9. Which we return 144elif (D2+D3==7 and D1+D4==9) or (D2+D3 ==9 and D1+D4==7):# This is one of the possible result we want where45return 1 # D2 & D3 is a pair and D1 and D4 is a pair. and one pair46# adds up to 7 and the other adds up to 9. Which we return 147elif (D2+D4==7 and D1+D3==9) or (D2+D4 ==9 and D1+D3==7):# This is one of the possible result we want where48return 1 # D2 & D4 is a pair and D1 and D3 is a pair. and one pair49# adds up to 7 and the other adds up to 9. Which we return 150else: # (Unwanted result) None of the statement above is true /51return 0 # Isn't a result where one pair adds up to 7 and the other52# pair adds up to 9. So it returns 053sum([die_rolls() for i in range(1000000)])/1000000 # adds up all the '1's (wanted results) and divide it by the54# total number of trials. (in this case 1000000)55the output : 0.07366456575859