用Python实现彩票随机号码

import random

# 定义彩票规则
lottery_rule = {
    "红球": {"min": 1, "max": 33, "num": 6},
    "蓝球": {"min": 1, "max": 16, "num": 1}
}

# 生成红球号码
def generate_red_ball():
    red_ball_list = []
    for i in range(lottery_rule["红球"]["num"]):
        red_ball = random.randint(lottery_rule["红球"]["min"], lottery_rule["红球"]["max"])
        while red_ball in red_ball_list:
            red_ball = random.randint(lottery_rule["红球"]["min"], lottery_rule["红球"]["max"])
        red_ball_list.append(red_ball)
    red_ball_list.sort()
    return red_ball_list

# 生成蓝球号码
def generate_blue_ball():
    blue_ball = random.randint(lottery_rule["蓝球"]["min"], lottery_rule["蓝球"]["max"])
    return blue_ball

# 输出彩票号码
def print_lottery_number(red_ball_list, blue_ball):
    print("红球号码为:", end="")
    for red_ball in red_ball_list:
        print("{:02d}".format(red_ball), end=" ")
    print("蓝球号码为:{:02d}".format(blue_ball))
  

如果需要生成五组随机号码或者多组,只需要在主程序中多次调用 generate_red_ball() 和 generate_blue_ball() 函数即可。 下面是生成五组彩票号码的示例代码:

# 生成五组彩票号码并输出
if __name__ == '__main__':
    for i in range(5):
        red_ball_list = generate_red_ball()
        blue_ball = generate_blue_ball()
        print_lottery_number(red_ball_list, blue_ball)
        print()  # 换行

你可能感兴趣的:(python,开发语言,numpy)