import random from typing import List, Tuple, Optional # ==================== 可自定义的参数 ==================== NUM_TICKETS = 5 # 生成注数 # 红球过滤条件 (设为 None 表示不限制) RED_MIN_SUM = 90 # 红球和值下限 RED_MAX_SUM = 130 # 红球和值上限 RED_SIZE_RATIO = (3, 3) # 红球大小比 (大, 小) RED_ODD_EVEN_RATIO = (3, 3) # 红球奇偶比 (奇, 偶) RED_MIN_SPAN = 15 # 红球跨度下限 RED_MAX_SPAN = 28 # 红球跨度上限 # 蓝球过滤条件 (设为 None 表示不限制) BLUE_SIZE = None # 蓝球大小: 'big' 或 'small' BLUE_ODD_EVEN = None # 蓝球奇偶: 'odd' 或 'even' # ====================================================== RED_BALLS_RANGE = range(1, 34) BLUE_BALLS_RANGE = range(1, 17) # ---------- 核心判断函数 ---------- def check_red_balls(balls: List[int]) -> bool: """检查红球是否满足所有过滤条件""" if len(balls) != 6: return False total = sum(balls) if RED_MIN_SUM is not None and total < RED_MIN_SUM: return False if RED_MAX_SUM is not None and total > RED_MAX_SUM: return False span = max(balls) - min(balls) if RED_MIN_SPAN is not None and span < RED_MIN_SPAN: return False if RED_MAX_SPAN is not None and span > RED_MAX_SPAN: return False big = sum(1 for b in balls if b >= 17) small = 6 - big if RED_SIZE_RATIO and (big, small) != RED_SIZE_RATIO: return False odd = sum(1 for b in balls if b % 2 == 1) even = 6 - odd if RED_ODD_EVEN_RATIO and (odd, even) != RED_ODD_EVEN_RATIO: return False return True def check_blue_ball(ball: int) -> bool: """检查蓝球是否满足过滤条件""" if BLUE_SIZE == 'big' and ball < 9: return False if BLUE_SIZE == 'small' and ball > 8: return False if BLUE_ODD_EVEN == 'odd' and ball % 2 == 0: return False if BLUE_ODD_EVEN == 'even' and ball % 2 == 1: return False return True # ---------- 号码生成与显示 ---------- def generate_red_balls() -> Optional[List[int]]: """生成一组满足条件的红球,最多尝试10000次""" for _ in range(10000): candidate = sorted(random.sample(RED_BALLS_RANGE, 6)) if check_red_balls(candidate): return candidate return None def generate_blue_ball() -> Optional[int]: """生成一个满足条件的蓝球,最多尝试1000次""" for _ in range(1000): candidate = random.choice(list(BLUE_BALLS_RANGE)) if check_blue_ball(candidate): return candidate return None def format_ball(num: int) -> str: """将数字格式化为两位数""" return f"{num:02d}" def analyze_balls(red: List[int], blue: int) -> str: """分析并返回号码的统计信息""" total = sum(red) span = max(red) - min(red) big = sum(1 for b in red if b >= 17) small = 6 - big odd = sum(1 for b in red if b % 2 == 1) even = 6 - odd blue_size = "大" if blue >= 9 else "小" blue_oe = "奇" if blue % 2 == 1 else "偶" return (f"和值:{total:3d} | 跨度:{span:2d} | " f"大小比:{big}:{small} | 奇偶比:{odd}:{even} | " f"蓝球:{format_ball(blue)} ({blue_size},{blue_oe})") # ---------- 主程序 ---------- def main(): print("=" * 50) print("双色球号码生成器 (含自定义过滤)") print("=" * 50) success_count = 0 for i in range(NUM_TICKETS): red = generate_red_balls() blue = generate_blue_ball() if red is None or blue is None: print(f"第{i+1:02d}注: 生成失败 (未找到满足条件的号码)") continue red_str = ' '.join(format_ball(b) for b in red) blue_str = format_ball(blue) analysis = analyze_balls(red, blue) print(f"第{i+1:02d}注: [{red_str}] + [{blue_str}] -> {analysis}") success_count += 1 print("=" * 50) print(f"成功生成 {success_count}/{NUM_TICKETS} 注号码") if __name__ == "__main__": main()