雙色球中獎率分析(python)


我的雙色球分析程序
 
 
    因為平時有買雙色球,無奈屢買不中。故使用python完成了一個雙色球分析程序,來提高中獎概率。
下面就此程序,進行簡單說明。
    程序介紹:
1、根據以往中獎紀錄,進行分析,分析一段時間各個號碼出現的次數,時間段分為三個,分別為一周(3期)、一月(13期)、一年(153期)。
2、根據第一條分析結果,計算每個號出現的概率。
3、給每個時間段(一周、月、年)統計出的號碼出現概率,附一個權重,即用來表示,對最終分析結果的影響力。具體如下
     (1)統計一年中(153期)出現最多的號碼。
       (2)  統計一月中(13期)出現最多的號碼。
       (3)  統計一周中(3期)出現最少的號碼。
       原因:在雙色球的搖獎過程中,因為球的質量,形狀問題,可能會導致某些號碼總會比別的號碼出現幾率大。
      故長期呢,分析出現次數多的,短期內,分析出現次數少的,作為將來出現概率大的號碼。
     備注:雙色球,2年換一次球,一共4套球。
4、根據第三條計算方法,計算出每個號碼的總權重(即綜合各個時間段的權重),每個時間段的權重比率,采用黃金分割比率,為(4.236,1.618,2.618)作為(年、月、日)的比率。
5、根據以上統計結果,計算出將來出現幾率最大的號碼。
      主要功能:
1、計算將來中獎概率最大的號碼。
2、隨機產生一注。
3、自選號碼,計算自選號碼的中獎概率。

     代碼如下:

  1 #!d:/python32/python
2 #coding = <utf-8>
3 #雙色球計算
4 #Written by LiJunpin
5 import random
6 import re
7 class shuangseqiu():
8
9 def __init__(self):
10 self.quanzhong = [2.618,1.618,4.236]#權重,分別為一周統計數據,一月統計數據,一年統計數據與完全隨機權重的比值,表示對最后選出的號碼的影響力大小,此數據依據最偉大的黃金分割點制定。
11 #self.quanzhong = [1,1,1]
12 self.qishu = [4,13,153]#一周、一月、一年的彩票期數
13 self.data_long = ['W','M','Y']#期數時間范圍的長度表示
14 self.fle = '123.txt'#打開彩票文件
15 self.red_dict = {}#紅球存儲字典
16 self.blue_dict = {}#籃球字典
17 self.red_rate = {}#紅球出現率字典
18 self.blue_rate = {}#籃球出現率字典
19 self.red_qz = []#紅球權重
20 self.blue_qz = []#籃球權重
21 #初始化存儲字典
22 for i in range(1,34):
23 self.__initdict__(i,self.red_dict)
24 self.__initdict__(i,self.red_rate)
25 for i in range(1,17):
26 self.__initdict__(i,self.blue_dict)
27 self.__initdict__(i,self.blue_rate)
28 #print(self.red_dict,self.blue_dict)
29
30 def __initdict__(self,num ,color_dict):
31 color_dict[str(num)] = {}
32 for x in range(len(self.data_long)):
33 color_dict[str(num)][self.data_long[x]] = 0
34
35 def __count__(self,num, color_dict, data_long):#統計一個號碼出現的次數
36
37 #print(num)
38 color_dict[str(num)][data_long] += 1
39 #print (num,data_long)
40
41 def __countlst__(self,ball_lst,data_long):
42 #print(ball_lst)
43 #年數據存儲
44 for i in range(6):
45 self.__count__(ball_lst[i],self.red_dict, data_long)
46 self.__count__(ball_lst[6],self.blue_dict,data_long)
47 #print(self.red_dict)
48 #print(self.blue_dict)
49
50 def __readdata__(self):
51 #從文件讀取彩票中獎紀錄
52 ball_file = open(self.fle, 'r')
53 ball_lst = ball_file.readline()
54 for i in range(1,self.qishu[2]):
55 ball_lst = ball_file.readline().split()
56 #print(ball_lst)
57 #red = ball_list[5:11]
58 #blue = ball_list[11:]
59 #print(ball_lst)
60 ball_lst = ball_lst[4:]
61 if i <= self.qishu[0]:
62 self.__countlst__(ball_lst,self.data_long[0])
63 if i <= self.qishu[1]:
64 self.__countlst__(ball_lst,self.data_long[1])
65 self.__countlst__(ball_lst,self.data_long[2])
66 ball_file.close()
67 #print(self.red_dict)
68 #print('------------------------')
69 #print(self.blue_dict)
70
71 def __rateone__(self,qishu,data_long):#根據統計的一段時間內(以data_long為依據)的出現次數計算1-33,1-16的出現幾率
72 #計算總出現的次數
73 redall = qishu * 6
74 blueall = qishu * 1
75 #計算紅球出現率
76 for i in range(1,34):
77 self.red_rate[str(i)][data_long] = self.red_dict[str(i)][data_long]/redall
78 #計算籃球出現率
79 for i in range(1,17):
80 self.blue_rate[str(i)][data_long] = self.blue_dict[str(i)][data_long]/blueall
81 def __rate__(self):
82 for i in range(len(self.data_long)):
83 self.__rateone__(self.qishu[i],self.data_long[i])
84 #print(self.red_rate)
85 def __quanzhong__(self,num,color_rate):#計算num號碼對應的權重
86 value = (1/len(color_rate)- color_rate[str(num)][self.data_long[0]])* self.quanzhong[0]/sum(self.quanzhong)#一周出現概率高,那么后面,出現概率就會降
87 value += (color_rate[str(num)][self.data_long[1]] - 1/len(color_rate)) * self.quanzhong[1]/sum(self.quanzhong)
88 value += (color_rate[str(num)][self.data_long[2]] - 1/len(color_rate)) * self.quanzhong[2]/sum(self.quanzhong)
89 return value
90
91 def make_quanzhong(self):#生成整個權重的列表
92 for i in range(1,34):
93 self.red_qz.append([self.__quanzhong__(i,self.red_rate),i])
94 for i in range(1,17):
95 self.blue_qz.append([self.__quanzhong__(i,self.blue_rate),i])
96 #print(self.red_qz)
97 #print('--------------------------------------')
98 #print(self.blue_qz)
99
100
101
102 def init_data(self):#初始化讀入數據、生成概率和權重數據結構
103 self.__readdata__()
104 self.__rate__()
105 self.make_quanzhong()
106
107 def _str_format(self,s):#格式化輸入的字符串。形成數字列表
108 a = re.compile('\d+')
109 b = re.findall(a,s)
110 return b
111
112 def _find_qz(self,color_qz,num):#查找數字num的出現概率
113 for i in range(0,34):
114 if color_qz[i][1] == num:
115 return color_qz[i][0]
116
117 def _suiji_gl(self):#計算隨機概率
118 sjgl = 1
119 for x in range(28,34):
120 sjgl *= x
121 sjgl = 1/sjgl
122 return sjgl * 1/16
123
124 def gailv(self,s):#計算概率
125 lst = self._str_format(s)
126 #print(lst)
127 lst = [int(x) for x in lst]
128 #print(lst)
129 gl = 1
130 if not lst or len(lst)>7 or len(lst)<7 or max(lst)>33 or min(lst)<1:
131 return None
132 else:
133 for x in lst[:7]:
134 gl *= (1+self._find_qz(self.red_qz,x))
135 return gl * self._suiji_gl()
136
137 def caipiao_random(self):#隨機紅籃球列表
138 redball = sorted(random.sample(range(1,34),6))
139 blueball = random.sample(range(1,17),1)
140 return redball + blueball
141
142 def print_random(self):#打印隨機結果
143 random_ball = self.caipiao_random()
144 print('--------------------------------------------------')
145 print('紅球:',random_ball[:6],'籃球:',random_ball[6:7])
146 print('--------------------------------------------------')
147
148 def print_best_number(self):#輸出中獎概率最高的號碼
149 print('根據規則選出的最可能中獎的號碼如下:')
150 print('--------------------------------------------------')
151 print('紅球:',sorted(list(x[1] for x in sorted(self.red_qz)[-6:])),'籃球:',sorted(self.blue_qz)[-1][1])
152 print('--------------------------------------------------')
153
154
155 def print_gl(self):
156 print('輸入格式為-> \"(1,9,13,21,28,32)(14)\"')
157 c = input('請輸入:')
158 if not self.gailv(c):
159 print('輸入有誤!')
160 return
161 print('--------------------------------------------------')
162 print(' 隨機概率為:%0.10f%%'%(self._suiji_gl()*100))
163 print(' -------------------------')
164 print(' 自選概率為:%0.10f%%'%(self.gailv(c)*100))
165 print('--------------------------------------------------')
166 def print_all(shuangseqiu_obj):
167 print('''
168 |----------功能列表-------------|
169 |--1:打印中獎率最高代碼---------|
170 |--2:打印隨機號碼---------------|
171 |--3:自助選取號碼,打印中獎概率-|
172 |--q:退出-----------------------|
173 ''')
174
175 while True:
176 a = input('功能選擇:')
177 if a == '1':
178 shuangseqiu_obj.print_best_number()
179 elif a == '2':
180 shuangseqiu_obj.print_random()
181 elif a == '3':
182 shuangseqiu_obj.print_gl();
183 elif a == 'q':
184 break;
185 else:
186 print('輸入有誤')
187 print('謝謝使用')
188
189 if __name__ == '__main__':
190 b = shuangseqiu()
191 b.init_data()
192 print_all(b)
193
194


程序中使用雙色球歷史中獎號碼進行分析,文件為123.txt,文件格式如下:

序列    年份    期號    日期        紅1    紅2    紅3    紅4    紅5    紅6    藍球
1310    2012    2012025    2012/3/1    4    12    19    21    25    28    13    
1308    2012    2012023    2012/2/28    5    9    15    23    24    33    2    
1307    2012    2012022    2012/2/26    4    8    12    24    26    27    4    
1306    2012    2012021    2012/2/23    1    15    16    18    22    30    3    
1305    2012    2012020    2012/2/21    3    8    12    18    23    29    11    
1304    2012    2012019    2012/2/19    7    14    18    20    22    30    16    
1303    2012    2012018    2012/2/16    3    5    6    22    26    32    15    
1302    2012    2012017    2012/2/14    6    9    14    19    25    28    10    
1301    2012    2012016    2012/2/12    2    5    12    17    22    25    8    
1300    2012    2012015    2012/2/9    1    3    6    10    21    23    15    
1299    2012    2012014    2012/2/7    1    2    5    16    28    30    12    
1298    2012    2012013    2012/2/5    6    8    24    29    30    32    13    
1297    2012    2012012    2012/2/2    15    17    18    20    23    27    1    
1296    2012    2012011    2012/1/31    4    14    15    16    20    26    5    
1295    2012    2012010    2012/1/29    1    3    13    19    25    26    10    
1294    2012    2012009    2012/1/19    4    16    24    26    27    33    11    
1293    2012    2012008    2012/1/17    1    12    20    23    24    29    8    
1292    2012    2012007    2012/1/15    10    17    19    27    28    32    4    
1291    2012    2012006    2012/1/12    2    22    25    29    32    33    8    
1290    2012    2012005    2012/1/10    7    9    18    27    31    33    6    
1289    2012    2012004    2012/1/8    1    5    10    11    21    23    16    
1288    2012    2012003    2012/1/5    3    6    8    24    29    31    9    
1287    2012    2012002    2012/1/3    2    3    7    9    10    32    13    
1286    2012    2012001    2012/1/1    1    4    5    9    15    17    6    
1285    2011    2011153    2011/12/29    5    8    9    10    20    25    13    
1284    2011    2011152    2011/12/27    4    10    11    12    21    26    13    
1283    2011    2011151    2011/12/25    7    11    16    19    31    33    10    
1282    2011    2011150    2011/12/22    8    10    12    15    22    27    13    
1281    2011    2011149    2011/12/20    4    5    6    7    23    31    16    
1280    2011    2011148    2011/12/18    5    14    22    23    25    26    14    
1279    2011    2011147    2011/12/15    4    8    12    17    18    30    10    
1278    2011    2011146    2011/12/13    11    23    26    28    32    33    10    
1277    2011    2011145    2011/12/11    2    4    14    15    26    30    4    
1276    2011    2011144    2011/12/8    1    2    9    10    16    24    3   



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM