Contact Us:
If you have any inquiries, please feel free to contact us.  

 

联系我们:
如果您有任何问询,请随时联络我们。 

Tel: +1 415 625 3820

Email: Contact Us
Website: niaodian.com

 

电话: +1 415 625 3820

邮件: 联络我们
网站: niaodian.com

Python:超级大乐透随机号码的两种写法

自学Python以来,一直想自己写写彩票的随机选号程序。

经过一年懒散又不懈的学习,在把《Python编程:从入门到实践(第2版)》啃到第9章的时候,总算把这个小小的程序给写出来了。只是没想到居然就短短几行代码,果然这门语言还是厉害的。

用了2种方法实现,分别是用到sample和randint函数。

randint函数实现代码如下:

#通过sample和randint函数均可实现
#本例为randint函数实现方法 
#from random import sample
from random import randint

print('*'*22+'超级大乐透'+'*'*22)
print('#'*53)
def daletou(n):
	for i in range(n):
		red_no = []
		while len(red_no)<5:
			i = randint(1,35)
			i = str(i).zfill(2)
			if i not in red_no:
				red_no.append(i)
		red_no.sort()
		
		blue_no = []
		while len(blue_no)<2:
			i = randint(1,12)
			i = str(i).zfill(2)
			if i not in blue_no:
				blue_no.append(i)
		blue_no.sort()		

		print(f"红球:{red_no} ; 蓝球:{blue_no}")

daletou(5)

sample函数实现代码如下:

from random import sample as sp 

print('*'*22+'超级大乐透'+'*'*22)
print('#'*53)
def letou(n):
	for i in range(n):
		red_no=sp([str(i).zfill(2) for i in range(1,36)],5)
		blue_no=sp([str(i).zfill(2) for i in range(1,13)],2)
		red_no.sort()
		blue_no.sort()
		print(f"红球:{red_no};蓝球:{blue_no}")

letou(5)

整体来看,sample函数的效率高很多,代码寥寥几行就实现了,也没那么多逻辑判断。

然后,我按自己写的程序,机选了5注,去彩票店追加投注3倍了,昨晚开奖了。

************超级大乐透************
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
红球:09 19 22 26 31 | 蓝球:01 11
红球:08 16 18 20 28 | 蓝球:06 08
红球:04 09 11 20 27 | 蓝球:04 08
红球:15 28 29 30 31 | 蓝球:06 09
红球:06 24 28 29 31 | 蓝球:05 09
[Finished in 83ms]

不出意外的话,那还是瞎猫碰到死耗子了,中了一注5块。净亏损66.67%,嗯,可以的。

后续感觉可以在这个基础上加入深度学习的算法,再搞搞预测啥的,也算能解决一点实际问题。学习兴趣不就der一下上来了么。

Python路漫漫,希望少点懒散,多点敲代码的时间。与君共勉。

鳥電 (1)

鳥電创始人。即使我是机器人,也会有一颗爱你的♥

Comment