乐透中奖号码预测算法一则

很显然,谁都不能准确预测号码,本文的方法只是凭借个人可怜的概率知识、直觉、执念、灵感混合而得出,本人对结果的准确性不负任何责任。

英国的lotto规则是,从1-49这些数字里选6个,每期中奖号码为6个主号加1个额外号码,中5+1要比6个全中奖金少很多。我的算法是,从历史中奖号码里统计出每个数次的出现频率,不考虑顺序,因为每次的中奖号码都是从小到大排列的。统计出最高的6个和最低的6个,将最高的数字减去最低的,取绝对值,然后将各自的频率相减取绝对值再处以历史开奖次数,最后将这两个计算结果相乘,加到最低的数字上去。对于第二高和第二低的也是如此,以此类推。简单说就是取最低频率的数字,用最高频率的数字对其作微调。

其实这个算法比较简陋,没考虑中间的数字,这次写的比较草,以后会慢慢补充更复杂的算法,先上代码。

import operator
import math

#将历届数据读入列表
f = open('./lotto')
main_num_list = []
bonus_num_list = []
draws = 0
for line in f:
	main_num = (line.split('\t'))[0]
	main_num = main_num.split(' - ')
	for i in range(0,6):
		main_num[i] = (int(main_num[i]))
	main_num_list.append(main_num)
	bonus_num = int((line.split('\t'))[1])
	bonus_num_list.append(bonus_num)
	draws += 1

#生成一个key为1-49的字典,value记录该数字出现次数
candidate_main = {}
candidate_bonus = {}
for i in range(1,50):
	candidate_main[i] = 0
	candidate_bonus[i] = 0
for item in main_num_list:
	for number in item:
		candidate_main[number] += 1
for item in bonus_num_list:
	candidate_bonus[item] += 1

#分析历史中奖号码中各数字出现的频率,并降序输出
print("Analysis of Main Numbers:\nNumber\tOccurrance")
counter = 0
high_freq_num = []
high_freq_occur = []
low_freq_num = []
low_freq_occur = []
for num in sorted(candidate_main, key=candidate_main.get, reverse=True):
	print("{0}\t{1}".format(num,candidate_main[num]))
	counter += 1
	if 1 <= counter <= 6:
		high_freq_num.append(num)
		high_freq_occur.append(candidate_main[num])
	if 44 <= counter <= 49:
		low_freq_num.append(num)
		low_freq_occur.append(candidate_main[num])

#计算预测的号码
predictions = []
for i in range(0,6):
	prediction = round(low_freq_num[5-i]+(math.fabs(high_freq_num[i]-low_freq_num[5-i])*(math.fabs((high_freq_occur[i]-low_freq_occur[5-i])/draws))))
	predictions.append(prediction)
print("Probably the next draw result would be:")
print(sorted(predictions))

#可以用同样方法处理Bonus Number,但是没有必要,因为只作开奖号码,不可挑选

历史数据来自官方网站,懒得用bs4分析html了,手动复制下来,稍微处理了一下,留下中奖号码,放在文本文件里,样例:

6 - 24 - 33 - 38 - 42 - 43     11
12 - 15 - 24 - 36 - 45 - 49     32
3 - 9 - 10 - 16 - 20 - 22     35
16 - 24 - 27 - 39 - 45 - 49     44
15 - 27 - 31 - 40 - 41 - 49     1
7 - 13 - 25 - 31 - 34 - 45     4

运行结果我就不贴出来了,很长。大家自己运行吧,万一我贴出来预测号码,结果中了那就太招摇了哈哈哈……

One Reply to “乐透中奖号码预测算法一则”

Leave a Reply

Your email address will not be published. Required fields are marked *