辅导Discrete Math、histograms留学生讲解、辅导Java/c++程序语言、讲解Python

- 首页 >> 其他
5/10/2019 Spring 2019 Discrete Math/Reference Materials/Pigs.sagews
file:///Users/billey/Downloads/Pigs.sagews.html 1/5
Spring 2019 Discrete Math/Reference Materials/Pigs.sagews
Date 2019-05-10T23:11:26
Project aa288497-fcac-43f6-91ba-dfd1aa1c10ed
Location Spring 2019 Discrete Math/Reference Materials/Pigs.sagews
Original file Pigs.sagews
1 ### Pass the Pigs
2 #Frequencies from the histograms made on Monday 5/6/2019
3 ## Dot, NoDot, Razor,Trottor, Sounter, Leaning Jowler, Other
4 F=matrix([[7,8,26,5,2,0,3], [37, 52, 55, 9, 4, 1,4], [30,22, 29, 3, 0, 0,0], [33, 44, 34, 4, 0,0,0], [29, 34, 40, 12, 1, 0,0],
6 ## Score function: Feel free to modify or reimplement this code however you want to.
8 def Score (x, roll):
9 if roll == [1,1]: ## sider with dots showing
10 return x+1
11 elif roll == [2,2]: ## sider with nodots showing
12 return x+1
13 elif roll == [3,3]: ## double razor
14 return x+ 20
15 elif roll == [4,4]: ## double trotter
16 return x+20
17 elif roll == [5,5]: ## double snouter
18 return x+40
19 elif roll == [6,6]: ## double leaning jowler
20 return x+60
21 elif roll == [1,2]: #pig out
22 return 0
23 elif roll == [1,3]: # razor
24 return x + 5
25 elif roll == [1,4]: # trotter
26 return x + 5
27 elif roll == [1,5]: #snouter
28 return x + 10
29 elif roll == [1,6]: # leaning jowler
30 return x + 15
31 elif roll == [2,3]: # razor
32 return x + 5
33 elif roll == [2,4]: # trotter
34 return x + 5
35 elif roll == [2,5]: #snouter
36 return x + 10
37 elif roll == [2,6]: # leaning jowler
38 return x + 15
39 elif roll == [3,4]: # razor plus trotter
40 return x + 5+5
41 elif roll == [3,5]: #razor plus snouter
42 return x + 5+10
43 elif roll == [3,6]: #razor plus leaning jowler
44 return x + 5+ 15
45 elif roll == [4,5]: #trotter plus snouter
46 return x + 5+10
47 elif roll == [4,6]: #trotter plus leaning jowler
48 return x + 5+ 15
49 elif roll == [5,6]: #trotter plus leaning jowler
50 return x + 10+ 15
51 elif (roll[0] == 7) or (roll[1] == 7 ): #other rolled, reroll with no penalty
52 return x5/10/2019 Spring 2019 Discrete Math/Reference Materials/Pigs.sagews
file:///Users/billey/Downloads/Pigs.sagews.html 2/5
53 elif roll[0]>roll[1]:
54 return Score(x,[roll[1],roll[0]])
55 counts=[sum(F.column(j)) for j in range(6)] # not counting other rolls
56 counts
[276, 363, 413, 109, 35, 6]
57 P=[counts[j]/sum(counts) for j in range(6)]
58 [float(p) for p in P]
[0.22961730449251247, 0.3019966722129784, 0.34359400998336104, 0.09068219633943428, 0.029118136439267885, 0.004991680532445923]
59 sum(P)
60 random()
0.7302587689166408
61 CDF = [sum(P[i] for i in range(j)) for j in range(7)]
62 [float(c) for c in CDF] #cumulative distribution function for 7 possibilities is given by list of 7 numbers
[0.0, 0.22961730449251247, 0.5316139767054908, 0.8752079866888519, 0.9658901830282862, 0.9950083194675541, 1.0]
63 def pigroll():
64 x = random()
65 roll =-1
66 for i in range(7):
67 if x>CDF[i]:
68 roll = roll + 1
69 return roll
70 [pigroll() for i in range(100)]
[1, 2, 4, 2, 1, 1, 1, 0, 2, 2, 1, 3, 2, 1, 1, 2, 1, 1, 3, 2, 1, 1, 0, 1, 2, 0, 1, 0, 0, 1, 3, 2, 1, 1, 0, 2, 3, 0, 1, 3, 1, 1, 0, 1, 1
4, 1, 1, 2, 1, 2, 0, 0, 0, 1, 2, 0, 0, 2, 2, 2, 2, 4, 3, 1, 2, 1, 1, 2, 0, 2, 2, 2, 1, 1, 5, 5, 1, 1, 2, 2, 0, 3, 1, 2, 3, 0, 2, 0, 2,
1, 1, 2, 2, 4, 3, 2, 3]
71 Test = [0 for i in range(6)] ## Looks like we are getting some numbers larger and some smaller than they should be
72 for i in range(1000):
73 temp = pigroll()
74 Test[temp] = Test[temp]+1
75 Test
[247, 307, 321, 95, 27, 3]
76 [float(p) for p in P]
[0.22961730449251247, 0.3019966722129784, 0.34359400998336104, 0.09068219633943428, 0.029118136439267885, 0.004991680532445923]
77 Test2 = [0 for i in range(6)] ## looks better!
78 for i in range(10000):
79 temp = pigroll()
80 Test2[temp] = Test2[temp]+1
81 Test2
[2257, 3060, 3444, 862, 339, 38]
82 roll = [pigroll()+1, pigroll()+1]
83
84 roll, Score(3, roll)
([1, 2], 0)
85 Record_Scores=[]
86 for i in range(100):
87 ## simulation of rolling two pig dice
88 roll = [pigroll()+1, pigroll()+1]
89 Record_Scores.append(Score(0, roll))
90 max(Record_Scores)
20
91 Record_Scores[3]
05/10/2019 Spring 2019 Discrete Math/Reference Materials/Pigs.sagews
file:///Users/billey/Downloads/Pigs.sagews.html 3/5
92 Totals = [0 for i in range(61)]
93 for i in Record_Scores:
94 Totals[i]=Totals[i]+1
95 Totals
[12, 17, 0, 0, 0, 45, 0, 0, 0, 0, 7, 0, 0, 0, 0, 2, 0, 0, 0, 0, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
96 Q=[Totals[i]/100 for i in range(61)]
97 ## probability of +0, +1, +2 ,...
98 Q
[3/25, 17/100, 0, 0, 0, 9/20, 0, 0, 0, 0, 7/100, 0, 0, 0, 0, 1/50, 0, 0, 0, 0, 17/100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
99 x=var('x')
102 ##What is the formula for expected score?
103 ### E[NextScore(x)]= sum over all possible values of Score(x, roll) times prob it occur.
104 ExpectedScore=0+sum((x+i)*Q[i] for i in range(1,61))
105 L=ExpectedScore
106 L
22/25*x + 341/50
107 plot(L , (x,0,50))
108 plot(L, (x,0,60))+ plot(x , (x,0,60), color='red')5/10/2019 Spring 2019 Discrete Math/Reference Materials/Pigs.sagews
file:///Users/billey/Downloads/Pigs.sagews.html 4/5
109 float((341/50)*(25/3))
(19, 23.54)5/10/2019 Spring 2019 Discrete Math/Reference Materials/Pigs.sagews
file:///Users/billey/Downloads/Pigs.sagews.html
116 def sara_billey_slow_and_steedy (opponentscore, myscore, myturnscore,round):
117 if myturnscore>10:
122 sara_billey_slow_and_steedy (99,10,2,4)
123 [sara_billey_slow_and_steedy(99,10,i,4) for i in range(5,15)]
[True, True, True, True, True, True, False, False, False, False]
generated 2019-05-10T23:11:26 on CoCalc