这几天和同事一起买了大乐透,无聊使用python模拟大乐透的号码,代码如下:
import random
import threading
def create_first_five(simulate_nums):
first_five_nums = []
'''生成5个不重复随机数'''
for i in range(simulate_nums):
list = []
while True:
temp = random.randint(1, 35)
if temp not in list:
list.append(temp)
first_five_nums.append(temp)
if len(list) == 5:
break
print(list)
# print(first_five_nums)
'''统计各数字出现的次数'''
dict = {}
for key in first_five_nums:
dict[key] = dict.get(key, 0) + 1
# print(dict)
'''根据各数字出现的次数进行排序'''
first_five_nums_sort_temp = sorted(dict.items(), key=lambda x: x[1], reverse=True)
print(first_five_nums_sort_temp)
first_five_nums_sorted = []
for j in range(5):
# print(final_sort[j])
# print(final_sort[j][0])
first_five_nums_sorted.append(first_five_nums_sort_temp[j][0])
print("前5个号码为:" + str(first_five_nums_sorted))
return first_five_nums_sorted
def create_last_two(simulate_nums):
first_five_nums = []
'''生成2个不重复随机数'''
for i in range(simulate_nums):
list = []
while True:
temp = random.randint(1, 12)
if temp not in list:
list.append(temp)
first_five_nums.append(temp)
if len(list) == 2:
break
print(list)
# print(first_five_nums)
'''统计各数字出现的次数'''
dict = {}
for key in first_five_nums:
dict[key] = dict.get(key, 0) + 1
# print(dict)
'''根据各数字出现的次数进行排序'''
first_five_nums_sort_temp = sorted(dict.items(), key=lambda x: x[1], reverse=True)
print(first_five_nums_sort_temp)
last_two_nums_sorted = []
for j in range(2):
# print(final_sort[j])
# print(final_sort[j][0])
last_two_nums_sorted.append(first_five_nums_sort_temp[j][0])
print("后2个号码为:" + str(last_two_nums_sorted))
return last_two_nums_sorted
def creat_numbers(all_simulate_nums):
z1=create_first_five(all_simulate_nums)
# z1=str(z1).strip("]")
# z1=z1.strip("[")
z2=create_last_two(all_simulate_nums)
print("最终号码为:"+str(z1)+"+"+str(z2))
creat_numbers(10000000)
'''多线程'''
# threads = []
# t1 = threading.Thread(target=creat_numbers, args=(100,)) # target是要执行的函数名(不是函数),args是函数对应的参数,以元组的形式存在
# threads.append(t1)
# t2 = threading.Thread(target=creat_numbers, args=(100,))
# threads.append(t2)
# for t in threads:
# # t.setDaemon(True)
# t.start()