CoCalc Public FilesProject / S21 / Final Project Draft / Place Final Project Draft here.txtOpen with one click!
Author: Zhang Han
Views : 41
Compute Environment: Ubuntu 20.04 (Default)
1
#. our project is no. 4 in the list .
2
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.
3
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).
4
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.
5
6
solution 1 :
7
8
times=[] # int the trials that fit for the condition
9
import numpy as np
10
for e in range(0,1000000): # input the trials we want
11
res = np.random.randint(1, 7, size=1) # die 1 : the random number on 6 sides die roll once
12
res2 =np.random.randint(1, 7, size=1) # die 2 : the random number on 6 sides die roll once
13
res3= np.random.randint(1,7,size=1) # die 3 : the random number on 6 sides die roll once
14
res4=np.random.randint(1,7,size=1) # die 4 : the random number on 6 sides die roll once
15
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
16
i=1
17
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
18
i=1
19
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
20
i=1
21
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
22
i=1
23
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
24
i=1
25
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
26
i=1
27
else: # different from these 6 choices are unfit for the condition, then add 0
28
i=0
29
times.append(i) # keep trying the trials untill 1000000 times
30
pro=np.mean(times) # def probability is mean of all "1"
31
print(pro)
32
33
the output : 0.074081
34
35
36
solution 2 :
37
def die_rolls(): # defining die_roll for multiple runs
38
D1= np.random.randint(1,7) # die 1 : random number on a six number die
39
D2=np.random.randint(1,7) # die 2 : random number on a six number die
40
D3=np.random.randint(1,7) # die 3 : random number on a six number die
41
D4=np.random.randint(1,7) # die 4 : random number on a six number die
42
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
43
return 1 # D1 & D2 is a pair and D3 and D4 is a pair. and one pair
44
# adds up to 7 and the other adds up to 9. Which we return 1
45
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
46
return 1 # D2 & D3 is a pair and D1 and D4 is a pair. and one pair
47
# adds up to 7 and the other adds up to 9. Which we return 1
48
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
49
return 1 # D2 & D4 is a pair and D1 and D3 is a pair. and one pair
50
# adds up to 7 and the other adds up to 9. Which we return 1
51
else: # (Unwanted result) None of the statement above is true /
52
return 0 # Isn't a result where one pair adds up to 7 and the other
53
# pair adds up to 9. So it returns 0
54
sum([die_rolls() for i in range(1000000)])/1000000 # adds up all the '1's (wanted results) and divide it by the
55
# total number of trials. (in this case 1000000)
56
the output : 0.073664
57
58