优化红球的随机算法

main
roger_home_pc 2 years ago
parent a64bb0156c
commit f7a51e0544
  1. 36
      red_forecast.py

@ -1,3 +1,5 @@
import random
import pymysql
from pymysql.cursors import DictCursor
import json
@ -107,14 +109,15 @@ def get_red_forecast(_all_data, _red_data_dict, _red_data, range_num=1):
print(', '.join(sorted(_red_forecast_list[0:6])))
# 产生随机数
red_index_list = [n[0] - 1 for n in new_num(33, 6)[0:6]]
random_red = []
red_index_list = get_random_index()
_random_red = []
for index in red_index_list:
random_red.append(_red_forecast_list[index])
_random_red.append(_red_forecast_list[index])
_random_red = sorted(_random_red)
print('随机红球推荐:')
print(', '.join(sorted(random_red)))
print(', '.join(_random_red))
return _red_forecast_dict, _red_forecast_list
return _red_forecast_dict, _red_forecast_list, _random_red
def get_last_date_id():
@ -130,26 +133,41 @@ def update_red_forecast_db():
all_data = get_all_data()
red_data = get_all_red()
red_data_dict = get_all_red_with_id()
red_forecast_dict, red_forecast_list = get_red_forecast(all_data, red_data_dict, red_data)
red_forecast_dict, red_forecast_list, random_red = get_red_forecast(all_data, red_data_dict, red_data)
date_id = all_data[-1]['dateId']
params = 1
red_rate = json.dumps(red_forecast_dict)
red_suggest = json.dumps(sorted(red_forecast_list[0:6]))
conn = pymysql.connect(**db)
curser = conn.cursor(DictCursor)
last_date_id = get_last_date_id()
curser.execute("SELECT * FROM lottery.red_forecast WHERE dateId = %s", (last_date_id, ))
curser.execute("SELECT * FROM lottery.red_forecast WHERE dateId = %s", (last_date_id,))
have_duplicate = False if len(curser.fetchall()) == 0 else True
if not have_duplicate:
curser.execute(
"INSERT INTO lottery.red_forecast (`dateId`, `params`, `red_rate`, `red_suggest`) VALUES (%s, %s, %s, %s)",
(date_id, params, red_rate, red_suggest))
(date_id, params, red_rate, json.dumps(random_red)))
conn.commit()
curser.close()
else:
print('数据库中有重复记录,无需重复插入')
def get_random_index():
# 产生随机数
random_small, random_middle, random_big = 0, 0, 0
while random_middle <= 0:
random_big = random.randint(1, 3)
random_small = random.randint(1, 2)
random_middle = 6 - random_small - random_big
# print(random_small, random_middle, random_big)
small_red_index_list = [n[0] - 1 for n in new_num(11, random_small)[0:random_small]]
middle_red_index_list = [n[0] - 1 + 10 for n in new_num(11, random_middle)[0:random_middle]]
big_red_index_list = [n[0] - 1 + 20 for n in new_num(11, random_big)[0:random_big]]
red_index_list = sorted(small_red_index_list + middle_red_index_list + big_red_index_list)
# print(red_index_list)
return red_index_list
if __name__ == "__main__":
# 实现获取含有这个数的下一期或几期中数字的概率
# 实现获取含有所以数据的总出现概率

Loading…
Cancel
Save